使用 pillow 和 python3.8 调整 img 大小时出错

Getting error on resize img using pillow and python3.8

这是我的代码。

from PIL import Image
imgwidth = 800
img = Image.open('img/2.jpeg')
concat = (imgwidth/img.size[0])
height = float(img.size[1])*float(concat)
img = img.resize((imgwidth,height), Image.ANTIALIAS)
img.save('output.jpg')

我从博客中找到了这个例子。我有 运行 这段代码并出现以下错误,我是 python 的新手,不明白实际问题。

File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/PIL/Image.py", line 1929, in resize
    return self._new(self.im.resize(size, resample, box))
TypeError: integer argument expected, got float

as img.resize() 函数需要 height 的整数值,所以请确保高度应该是整数类型。 使用以下代码更新您的代码,希望它对您有用。

from PIL import Image
imgwidth = 800
img = Image.open('img/2.jpeg')
concat = float(imgwidth/float(img.size[0]))
height = int((float(img.size[1])*float(concat)))
img = img.resize((imgwidth,height), Image.ANTIALIAS)
img.save('output.jpg')