Python PIL Paste

9
我想使用PIL把一堆图片拼接在一起。但是当我运行代码blank.paste(img,(i*128,j*128))时,出现以下错误:ValueError: cannot determine region size; use 4-item box
我已经尝试过使用四个元素的元组(例如(128,128,128,128)),但是却给我返回了这个错误: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')

我怎么才能让它工作?

3个回答

6
这对我很有帮助,我使用的是Odoo v9和pillow 4.0。
我在我的ubuntu服务器上按照以下步骤操作:
# pip uninstall pillow
# pip install Pillow==3.4.2
# /etc/init.d/odoo restart

5
你没有正确地加载图片。内置函数 open 只是打开了一个新的文件描述符。要使用 PIL 加载图像,请改用 Image.open
from PIL import Image
im = Image.open("bride.jpg") # open the file and "load" the image in one statement

如果您有使用内置的`open`函数的理由,可以按照以下方式操作:
fin = open("bride.jpg") # open the file
img = Image.open(fin) # "load" the image from the opened file

使用PIL,“加载”图像意味着读取图像头。PIL是惰性的,因此直到需要时才会加载实际的图像数据。
此外,考虑使用os.path.join而不是字符串连接。

好的,谢谢。我就知道这肯定是些愚蠢的小错误。 - user2901745

2

对我来说,上述方法都没有起作用。

在检查了image.py之后,我发现image.paste(color)需要多传一个参数,像这样image.paste(color, mask=original)。将其更改为此方式后,它对我很有效:

image.paste(color, box=(0, 0) + original.size)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接