2013-10-21 95 views
6

我想将一堆图像与PIL一起粘贴。出于某种原因,当我运行线blank.paste(img,(i*128,j*128))我得到以下错误:(EX(128128128128))ValueError: cannot determine region size; use 4-item boxPython PIL Paste

我试图与它搞乱,并使用一个元组有4个元素,比如它说,但它给了我这个错误: SystemError: new style getargs format but argument is not a tuple

每张图片都是128x,并且命名风格为“x_y.png”,其中x和y从0到39.我的代码如下。

from PIL import Image 

loc = 'top right/' 
blank = Image.new("RGB", (6000,6000), "white") 

for x in range(40): 
    for y in reversed(range(40)): 
     file = str(x)+'_'+str(y)+'.png' 
     img = open(loc+file) 
     blank.paste(img,(x*128,y*128)) 

blank.save('top right.png') 

我该如何得到这个工作?

回答

1

图像正确无误载入图像。内置函数open只是打开一个新的文件描述符。要加载与PIL的图像,使用Image.open代替:

from PIL import Image 
im = Image.open("bride.jpg") # open the file and "load" the image in one statement 

如果你有一个理由使用内置的开放,然后做这样的事情:

fin = open("bride.jpg") # open the file 
img = Image.open(fin) # "load" the image from the opened file 

随着PIL,“加载”图像意味着读取图像标题。 PIL是懒惰的,所以它不会加载实际的图像数据直到它需要。

此外,请考虑使用os.path.join而不是字符串连接。

+0

这工作,谢谢。我知道这会是一件愚蠢的事情。 – user2901745

+0

对于os.path.join建议+1。 – volvox

3

这工作对我来说,我使用Odoo V9,我有枕头4.0。

我做到了我的服务器的步骤与Ubuntu:

# pip uninstall pillow 
# pip install Pillow==3.4.2 
# /etc/init.d/odoo restart 
+0

你的回答对我非常有帮助,非常感谢 –