Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Pillow's thumbnail method returning None

I have a small script I'm using to batch resize images using Python's Pillow library. The script works for the resize method however the aspect ratio changes which distorts the image so I'm trying to test the same script with the thumbnail method.

I'm quite perplexed as it seems from the docs and other stack questions that I can just swap the resize method for the thumbnail method. However when I switch to thumbnail, a none-type object is returned.

I'm using Python 3.5 and Pillow 5.0. Any ideas?

from PIL import Image
import glob

file_list = glob.glob('images_pre/*.jpg')

for f in file_list:
        image = Image.open(f)
        # image = image.resize((170, 170), Image.ANTIALIAS)
        print('image pre: ' + str(image))
        image = image.thumbnail((128, 128), Image.BICUBIC)
        print('image post: ' + str(image))
        file_name = f.split('/')[-1]
        try:
                image.save('images_post/'+file_name, "JPEG")
        except AttributeError:
                print('image failed to save: ' + str(image))
like image 997
Braden Holt Avatar asked Apr 06 '18 04:04

Braden Holt


1 Answers

Image.thumbnail modifies the image in place and doesn't return anything.

The docs http://pillow.readthedocs.io/en/5.1.x/reference/Image.html#PIL.Image.Image.thumbnail say:

Note that this function modifies the Image object in place. If you need to use the full resolution image as well, apply this method to a copy() of the original image.

like image 94
Hugo Avatar answered Oct 11 '22 06:10

Hugo