PIL
libraryPython Imaging Library or PIL
is an image processing library in python. This library supports a wide range of file formats, is designed for an efficient internal representation of image data, and provides powerful image processing capabilities.
Image
moduleA class with the same name is provided by the Image
module and is used to represent an image in PIL
. The module also has factory functions, such as those for loading pictures from files and creating new images.
thumbnail
functionThe thumbnail
function is used to generate the thumbnail version of the given image no larger than itself. This method determines an acceptable thumbnail size to retain the image’s aspect ratio. This method modifies the image in-place, it modifies the original image. If the original image needs to be retained as is, then apply this function on the copy of the image.
Image.thumbnail(size, resample=Resampling.BICUBIC)
size
: It is the size of the thumbnail.resample
: It is the resampling filter. There are multiple filters in Image.Resampling
that can be used.Let’s look at the code below:
from PIL import Imageimport matplotlib.pyplot as pltimg = Image.open("educative.jpeg")SIZE = (75, 75)img.thumbnail(SIZE)img.save('educative_thumb.png')
Image
module from PIL
.educative.jpeg
into memory using open()
in Image
module.thumbnail()
method on the image object loaded in the Line 3. There is no return value as the operation is in-place.