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))
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 acopy()
of the original image.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With