Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python PIL TypeError: integer argument expected, got float

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?

like image 863
user2040938 Avatar asked Jul 08 '13 15:07

user2040938


2 Answers

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)
like image 94
Mark Ransom Avatar answered Nov 24 '22 04:11

Mark Ransom


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.

like image 23
Josh Avatar answered Nov 24 '22 04:11

Josh