Python 如何使用枕头将图像粘贴到较大的图像上?

Python 如何使用枕头将图像粘贴到较大的图像上?,python,python-imaging-library,pillow,Python,Python Imaging Library,Pillow,我有一个相当简单的代码文件: from PIL import Image til = Image.new("RGB",(50,50)) im = Image.open("tile.png") #25x25 til.paste(im) til.paste(im,(23,0)) til.paste(im,(0,23)) til.paste(im,(23,23)) til.save("testtiles.png") 但是,当我尝试运行它时,会出现以下错误: Traceback (most recen

我有一个相当简单的代码文件:

from PIL import Image
til = Image.new("RGB",(50,50))
im = Image.open("tile.png") #25x25
til.paste(im)
til.paste(im,(23,0))
til.paste(im,(0,23))
til.paste(im,(23,23))
til.save("testtiles.png")
但是,当我尝试运行它时,会出现以下错误:

Traceback (most recent call last):
    til.paste(im)
  File "C:\Python27\lib\site-packages\PIL\Image.py", line 1340, in paste
    self.im.paste(im, box)
ValueError: images do not match

是什么导致了这个错误?它们都是RGB图像,文档没有说明此错误。

问题在于第一次粘贴-根据PIL文档(),如果没有传递“box”参数,则图像的大小必须匹配

编辑: 我实际上误解了文档,你是对的,它不在那里。但从我在这里尝试的情况来看,似乎没有第二个论点,尺寸必须匹配。如果要保持第二个图像的大小并将其放置在第一个图像的左上角,只需执行以下操作:

...
til.paste(im,(0,0))
...

所以我可能会晚一点,但也许这有助于后面的人:

当我遇到同样的问题时,我找不到太多关于它的信息。 所以我写了一个片段,将一个图像粘贴到另一个图像中

def PasteImage(source, target, pos):

    # Usage:
    # tgtimg = PIL.Image.open('my_target_image.png')
    # srcimg = PIL.Image.open('my_source_image.jpg')
    # newimg = PasteImage(srcimg, tgtimg, (5, 5))
    # newimg.save('some_new_image.png')
    #

    smap = source.load()
    tmap = target.load()
    for i in range(pos[0], pos[0] + source.size[0]): # Width
        for j in range(pos[1], pos[1] + source.size[1]): # Height
            # For the paste position in the image the position from the top-left
            # corner is being used. Therefore 
            # range(pos[x] - pos[x], pos[x] + source.size[x] - pos[x])
            # = range(0, source.size[x]) can be used for navigating the source image.

            sx = i - pos[0]
            sy = j - pos[1]

            # Change color of the pixels
            tmap[i, j] = smap[sx, sy]

    return target
不一定是最好的方法,因为它大约需要O(N^2),但它适用于小图像。也许有人可以改进代码以提高效率

我做得很匆忙,所以它也没有输入验证。 只需知道源图像的宽度和高度必须小于或等于目标图像的宽度和高度,否则它将崩溃。
您也只能粘贴整个图像,而不能粘贴部分或非矩形图像。

通常当两个图像的模式不匹配时。我实际上使用的是Pill,PIL fork:不过,这是解决方案。非常感谢。