How to thumbnail with Python and Pillow

Install pillow

pip install Pillow

Snippet of code

from PIL import Image

SIZE = (315, 320)

im = Image.open(image_path)
im.convert('RGB')
im.thumbnail(SIZE, Image.ANTIALIAS)
im.save(thumb_path, 'JPEG', quality=80)

# get file size in bytes
import os
print(os.stat(thumb_path).st_size)

There are three different function to resize image

  • Image.resize changes the aspect ratio of the image.
  • Image.thumbnail keeps the aspect ratio of the image and scales it to the defined area.
  • ImageOps.fit keeps the aspect ratio and crops the image to the size of the defined area.
comments powered by Disqus