2013-07-08 47 views
3

我不斷收到此錯誤運行在Python 3.x的糊狀腳本時: 類型錯誤:預期的整數參數,得到了浮Python的PIL類型錯誤:整數參數預期,有浮

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?

回答

7

在Python 3,從你需要使用//,而不是/一個部門得到一個整數結果:

offset=((bg_w-img_w)//2,(bg_h-img_h)//2) 
1

我的猜測是,它不喜歡這一行

offset=((bg_w-img_w)/2,(bg_h-img_h)/2) 

所以我會嘗試像

offset=((bg_w-img_w)//2,(bg_h-img_h)//2) 

但它似乎是有人只是打敗了我。

+0

修好了!!! ('C:\ Mine','r') img_w,img_h = img.size background = Image.new('RGBA',(1440,900),( (img,255,255,255,255)) bg_w,bg_h = background.size offset =(int((bg_w-img_w)/ 2),int((bg_h-img_h)/ 2)) background.paste(img,偏移) background.save('new.jpg') 將偏移量數據添加到int中。 – user2040938

相關問題