Python PIL TypeError:预期为整数参数,浮点数 [英] Python PIL TypeError: integer argument expected, got float

查看:180
本文介绍了Python PIL TypeError:预期为整数参数,浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python 3.x中运行粘贴脚本时,我不断收到此错误: TypeError:预期为整数参数,为浮点数

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

错误味精:

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

我看到应该有一个整数,但是最后得到一个浮点数.我该怎么做使其成为int呢?

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?

推荐答案

在Python 3中,要从除法获得整数结果,您需要使用//而不是/:

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)

这篇关于Python PIL TypeError:预期为整数参数,浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆