How to resize images in Python using Pillow

Learn how to resize images and create thumbnails in Python using Pillow - the Python Imaging Library (PIL).

How to resize images in Python using Pillow
Picture by Picjumbo, Pexels.
5 minutes to read

Pillow is a fork of the Python Imaging Library (PIL) and is one of the most useful Python tools for resizing images. Pillow can actually perform a very wide range of image manipulation tasks, from obtaining information on image formats, to convert the image type, or cropping or resizing images.

In this simple project we’ll go through some example code that you can use to bulk resize images to thumbnails or other sizes using Python.

Import the packages

To get started, open a Jupyter notebook and import the os and PIL packages. We’ll be using the Image class from Pillow. This includes a range of useful functions you can use to open and interact with images stored on your computer or server.

import os
from PIL import Image

Get a list of images inside a directory

Next, you’ll need to place a selection of images in a directory on your computer or notebook server, so we can access them and resize them or convert them to thumbnails. I’ve stored my images in a directory called images/ and have stored this in a variable called path.

path = 'images/'

Now I’ll use the listdir() function from the Python OS package to obtain the filename of each of the files inside my folder. The function returns a Python list, so we’ll store this in a variable called images.

images = os.listdir(path)
images
['tranmautritam.jpg',
 'credit-card.jpg',
 'roberto-nickson.jpg',
 'gapandas4.jpg']

Rescale an image to a maximum width or height

Now we’ll use Pillow to resize an image to a thumbnail and constrain its proportions, or maintain its aspect ratio, so it doesn’t look squished.

We’ll first set two variables - max_width and max_height to hold the maximum width and height for each image. You can, of course, use any sizes. You don’t have to go with a thumbnail. We’ll then use Image.open() to load up an image using Pillow and we’ll store the data in a variable called im.

max_width = 100
max_height = 100
im = Image.open(path + 'gapandas4.jpg')
im

png

Next, we’ll create a tuple called max_size into which we’ll pass our max_width and max_height values and we’ll call the thumbnail() functions and pass the tuple as an argument.

max_size = (max_width, max_height)
im.thumbnail(max_size)

The resized image is currently assigned to the im variable, so we’ll use save() to write the new resized image back to a new directory called /thumbnails. You could, if you prefer, rename the image instead.

im.save(path + '/thumbnails/' + 'gapandas4.jpg')

Now, we’ll open the resized image and check the size. As you can see, it’s been resized to a maximum width or height of 100 pixels and its proportions have been constrained.

im = Image.open(path + '/thumbnails/' + 'gapandas4.jpg')
im

png

Resize a directory of images to a maximum width or height

If you’ve got lots of images you want to resize or convert to thumbmails, you can use a for loop to repeat the process for each image found. In the example below, we’ll load each image found, create a thumbnail, and then save that to the /thumbnails directory.

for image in images:
    im = Image.open(path + image)
    im.thumbnail((max_width, max_height))
    im.save(path + '/thumbnails/' + image)

If you want to get back information about the images processed, such as their name, size, path, width, or height, you can combine some of the functions from os and return the data to a dictionary.

for image in images:
    size = os.path.getsize(path + '/thumbnails/' + image)
    im = Image.open(path + '/thumbnails/' + image)
    width, height = im.size
    
    data = {
        'image': image,
        'path': path + image,
        'size': size, 
        'width': width,
        'height': height
    }
    
    print(data)
{'image': 'tranmautritam.jpg', 'path': 'images/tranmautritam.jpg', 'size': 2792, 'width': 100, 'height': 67}
{'image': 'credit-card.jpg', 'path': 'images/credit-card.jpg', 'size': 2102, 'width': 100, 'height': 64}
{'image': 'roberto-nickson.jpg', 'path': 'images/roberto-nickson.jpg', 'size': 2789, 'width': 100, 'height': 67}
{'image': 'gapandas4.jpg', 'path': 'images/gapandas4.jpg', 'size': 2411, 'width': 100, 'height': 67}

Matt Clarke, Friday, September 02, 2022

Matt Clarke Matt is an Ecommerce and Marketing Director who uses data science to help in his work. Matt has a Master's degree in Internet Retailing (plus two other Master's degrees in different fields) and specialises in the technical side of ecommerce and marketing.