note.nkmk.me

Resize images with Python, Pillow

Posted: 2020-10-20 / 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 Module — Pillow (PIL Fork) 4.0.0 documentation

This post 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 post for installation and basic usage of Pillow (PIL).

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

Sponsored Link

How to use Image.resize()

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

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

Filters
- NEAREST
- BOX
- BILINEAR
- HAMMING
- BICUBIC
- LANCZOS
Concepts — Pillow (PIL Fork) 4.0.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((int(img.width / 2), int(img.height / 2)))
img_resize_lanczos.save('data/dst/lena_pillow_resize_half.jpg')

Because the size should be specified as an integer, int() is used. You can use // instead of / and int().

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().

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

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

for f in files:
    img = Image.open(f)
    img_resize = img.resize((int(img.width / 2), int(img.height / 2)))
    title, ext = os.path.splitext(f)
    img_resize.save(title + '_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:
    title, ext = os.path.splitext(f)
    if ext in ['.jpg', '.png']:
        img = Image.open(f)
        img_resize = img.resize((int(img.width / 2), int(img.height / 2)))
        img_resize.save(title + '_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((int(img.width / 2), int(img.height / 2)))
        title, ext = os.path.splitext(f)
        img_resize.save(title + '_half' + ext)
    except OSError as e:
        pass
Sponsored Link
Share

Related Categories

Related Posts