简体   繁体   中英

Getting error on resize img using pillow and python3.8

Here is my code.

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

I found this example from a blog. I have run this code and got the bellow error, I am new in python and not understand the actual problem.

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() function expect integer value for height , so please make sure height should be an integer type. Update your code with the below code, I hope it should be working for you.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM