Resize images with Python, Pillow

Modified: | Tags: Python, Pillow, Image Processing

Image module of the image processing library Pillow (PIL) provides resize() method to resize images.

Image.resize(size, resample=0)
Returns a resized copy of this image.
Image.resize() - Pillow (PIL Fork) 10.2.0 documentation

This article describes the following contents.

  • How to use Image.resize()
  • Sample code for batch resizing
    • Resize only files with specific extensions
    • Resize all image files that Pillow supports

See the following article for installation and basic usage of Pillow (PIL).

Use crop() to crop a part of the image.

How to use Image.resize()

Pass a tuple of (width, height) as the argument size.

Pass the filter used for resampling as the argument resample. There are the following 6 types. If omitted, NEAREST is used by default.

Filters
- NEAREST
- BOX
- BILINEAR
- HAMMING
- BICUBIC
- LANCZOS
Concepts - Filters - Pillow (PIL Fork) 10.2.0 documentation

from PIL import Image

img = Image.open('data/src/lena_square.png')

img_resize = img.resize((256, 256))
img_resize.save('data/dst/lena_pillow_resize_nearest.jpg')

img_resize_lanczos = img.resize((256, 256), Image.LANCZOS)
img_resize_lanczos.save('data/dst/lena_pillow_resize_lanczos.jpg')

BICUBIC and LANCZOS take longer to process than NEAREST, but the quality is better.

The left image is resized by NEAREST filter and the right image is resized by LANCZOS filter.

Pillow resize Nearest Pillow resize Lanczos

In the above example, the image size is fixed as (256, 256). To specify it based on the size of the original image, do as follows.

img_resize = img.resize((img.width // 2, img.height // 2))
img_resize_lanczos.save('data/dst/lena_pillow_resize_half.jpg')

Because the size should be specified as an integer, // is used.

Sample code for batch resizing

For batch resizing, get the path string of the files in the target folder (directory) with glob(), resize them with resize(), and save them with the new file name with save(). You can create the destination directory with makedirs().

Resize only files with specific extensions

The sample code to extract only the files with the extension jpg is as follows:

import os
import glob
from PIL import Image

dst_dir = 'data/temp/images_half'
os.makedirs(dst_dir, exist_ok=True)

files = glob.glob('./data/temp/images/*.jpg')

for f in files:
    img = Image.open(f)
    img_resize = img.resize((img.width // 2, img.height // 2))
    root, ext = os.path.splitext(f)
    basename = os.path.basename(root)
    img_resize.save(os.path.join(dst_dir, basename + '_half' + ext))

If you want to get files with multiple extensions, you can select them after extracting the paths of all files with glob().

files = glob.glob('./data/temp/images/*')

for f in files:
    root, ext = os.path.splitext(f)
    if ext in ['.jpg', '.png']:
        img = Image.open(f)
        img_resize = img.resize((img.width // 2, img.height // 2))
        basename = os.path.basename(root)
        img_resize.save(os.path.join(dst_dir, basename + '_half' + ext))

Resize all image files that Pillow supports

If you want to resize all the image files that Pillow supports instead of extracting by extension, use try ... except ... to handle exceptions. It's easier than explicitly specifying multiple extensions.

files = glob.glob('./data/temp/images/*')

for f in files:
    try:
        img = Image.open(f)
        img_resize = img.resize((img.width // 2, img.height // 2))
        root, ext = os.path.splitext(f)
        basename = os.path.basename(root)
        img_resize.save(os.path.join(dst_dir, basename + '_half' + ext))
    except OSError as e:
        pass

Related Categories

Related Articles