Harris Harris - 6 months ago 24
Python Question

Python not resizing height of an image

I want to resize some images and here is my code.

import os
from PIL import Image

size = 300, 300

for f in os.listdir('.'):
if f.endswith('.png'):
i = Image.open(f)
fn, fext = os.path.splitext(f)
i.thumbnail(size, Image.ANTIALIAS)
i.save('output/{}{}'.format(fn, fext))


The code is working fine and it resizes all my image to a width of 300px, but the height did not resize.

Can anyone tell me why?

Rob Rob
Answer

Image.thumbnail() is designed to keep the aspect ratio of the original image. If you want the output image to be exactly 300x300 px, use Image.resize() instead.