Python PIL – Create a thumbnail

In this post we will be creating a Python script that will create a thumbnail of an image, to do this we will be using the Python Imaging Library also known as PIL enabling us to image processing capabilities.

See the sample of Python code where we utilise the PIL library to create a thumbnail of an image and save it as a new file. To help us achieve this we utilize the ‘Image.thumbnail()’ method which modifies the image to contain a thumbnail version of itself, whilst at the same time preserving its aspect ratio. The thumbnail will be no larger than the given size which in this case has been set 120 pixels in width and 120 pixels in height.

from PIL import Image

img = Image.open('image.png')
img.thumbnail((128, 128))
img.save('thumbnail.png')

Take a look at some of our other content around the Python programming language by clicking here.

Leave a Reply