One important hint, though: Replace im.thumbnail(size)īy default, PIL uses the Image.NEAREST filter for resizing which results in good performance, but poor quality. I also recommend using PIL's thumbnail method, because it removes all the ratio hassles from you. Img = img.resize((basewidth,hsize), Image.ANTIALIAS) Hsize = int((float(img.size)*float(wpercent))) Change "basewidth" to any other number to change the default width of your images. With Python, we used Image.open to resize and read an image using PIL and maintain its aspect ratio. It does this by determining what percentage 300 pixels is of the original width (img.size) and then multiplying the original height (img.size) by that percentage. This script will resize an image (somepic.jpg) using PIL (Python Imaging Library) to a width of 300 pixels and a height proportional to the new width. Print "cannot create thumbnail for '%s'" % infile ![]() (M, N, 3): an image with RGB values (0-1 float or 0-255 int). PIL resize image while maintaining aspect ratio Raw no-crop-resize.py /usr/bin/env python - coding: utf-8 - author 'olucurious' from PIL import Image FIN 'imh.JPG' size (480, 480) the image is resized to fit into a 480x480 square padded by white areas image Image. The values are mapped to colors using normalization and a colormap. Scaling the old-fashioned way with grade-school. Supported array shapes are: (M, N): an image with scalar data. Usually, we want to resize an image but keep its aspect ratio intact, i.e. Syntax: Image. Parameters: Xarray-like or PIL image The image data. Call the thumbnail () method on the Image instance, passing a tuple argument with two integers to specify the width and height you desire: image Image.open ('demoimage.jpg') image.thumbnail ( (400, 400)) image.save ('imagethumbnail.jpg') print (image. ![]() Image.resize() Returns a resized copy of this image. Perform steps 1 and 2 of the above procedure. The module also provides a number of factory functions, including functions to load images from files, and to create new images. ![]() Outfile = os.path.splitext(infile) + ".thumbnail" The Image module provides a class with the same name which is used to represent a PIL image. There is of course also a library method to do this: the method Image.thumbnail.īelow is an (edited) example from the PIL documentation. Then, compute a resize ratio by taking min(maxwidth/width, maxheight/height).
0 Comments
Leave a Reply. |