user2040938
user2040938

Reputation: 81

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?

Upvotes: 8

Views: 12421

Answers (2)

Josh
Josh

Reputation: 1052

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.

Upvotes: 2

Mark Ransom
Mark Ransom

Reputation: 308520

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)

Upvotes: 17

Related Questions