I keep getting this error when running a paste script in Python 3.x: TypeError: integer argument expected, got float
from PIL import Image
img=Image.open('C:\Mine.jpg','r')
img_w,img_h=img.size
background = Image.new('RGBA', (1440,900), (255, 255, 255, 255))
bg_w,bg_h=background.size
offset=((bg_w-img_w)/2,(bg_h-img_h)/2)
background.paste(img,offset)
background.save('C:\new.jpg')
Error MSG:
Traceback (most recent call last):
File "C:\Users\*****\workspace\Canvas Imager\src\Imager.py", line 7, in <module>
background.paste(img,offset)
File "C:\Python33\lib\site-packages\PIL\Image.py", line 1127, in paste
self.im.paste(im, box)
TypeError: integer argument expected, got float
I see that there is suppose to be an integer but is getting a float in the end. What can I do to make it int instead?
In Python 3, to get an integer result from a division you need to use //
instead of /
:
offset=((bg_w-img_w)//2,(bg_h-img_h)//2)
My guess would be that it doesn't like this line
offset=((bg_w-img_w)/2,(bg_h-img_h)/2)
so I would try something like
offset=((bg_w-img_w)//2,(bg_h-img_h)//2)
but it seems like someone just beat me to it.
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