The image is first cropped to the box, and the result is resized. Resize also has an optional parameter box that specifies a region to be resized. BICUBIC is probably the best option for general use (the default is NEAREST, but a modern computer can run BICUBIC pretty quickly and the quality improvement is quite noticeable).
The list of filters is ordered from lowest to highest quality, but the higher quality options tend to be a bit slower. These are similar to the options you might see in imaging application like Photoshop or GIMP. It can be NEAREST, BOX, BILINEAR, HAMMING, BICUBIC or LANCZOS. The function also has an optional resample parameter, that sets the resampling filter.
You can avoid this by making sure the aspect ratios are the same, or by using the thumbnail function, below. If the aspect ratio (the ratio of width to height) of the original image is different to the requested aspect ratio of the resized image, you will get some distortion as the image is stretched to fit a different shape. It will resize the image to fit within the specified size. Resize accepts a size parameter, a tuple (or list) containing the required width and height of the thumbnail in pixels. Alternatively you can specify the file type explicity as a second parameter:įrom PIL import Image im = Image. Save will use the file extension to determine the type of image file required, for example the. As with open it can accept a file name, Path object or a file object that has been opened to write.
The save function writes the image to file. This means it isn't all that useful in an actual application, but it can be handy during development. The main limitation is that after creating the window, your program no longer has control of it, so you have to close it manually. The show function (a method of the Image object, like many of the functions described here) pops up a window to display the image. It will determine the type of file automatically by looking at the file content. It can accept a file name (string), a Path object or an file object (eg created using the open function to create a readable file). The open function reads an image from disk.