如何使用枕头将图像粘贴到更大的图像上? [英] How to paste an image onto a larger image using Pillow?
问题描述
我有一个相当简单的代码文件:
I've got a fairly simple code file:
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")
但是,当我尝试运行它时,出现以下错误:
However, when I attempt to run it, I get the following error:
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图像,文档没有对此错误说任何话.
What is causing this error? They are both RGB images, the docs don't say anything about this error.
推荐答案
根据PIL文档( http://effbot.org/imagingbook/image.htm ),如果未传递"box"参数,则图像尺寸必须匹配.
The problem is in the first pasting - according to the PIL documentation (http://effbot.org/imagingbook/image.htm), if no "box" argument is passed, images' sizes must match.
我实际上误解了文档,您是对的,它不存在.但是从我这里的尝试来看,似乎没有传递第二个参数,大小必须匹配.如果要保持第二张图片的大小并将其放置在第一张图片的左上角,请执行以下操作:
I actually misunderstood the documentation, you are right, it's not there. But from what I tried here, it seems like passing no second argument, sizes must match. If you want to keep the second image's size and place it in the upper-left corner of the first image, just do:
...
til.paste(im,(0,0))
...
这篇关于如何使用枕头将图像粘贴到更大的图像上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!