Python 没有调整图片的高度

Python not resizing height of an image

我想调整一些图片的大小,这是我的代码。

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

代码工作正常,它将我所有图像的宽度调整为 300 像素,但高度没有调整。

谁能告诉我为什么?

Image.thumbnail()是为了保持原图的纵横比。如果您希望输出图像恰好为 300x300 像素,请改用 Image.resize()