Python 如何使用 Pillow 将图像粘贴到更大的图像上?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28407462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to paste an image onto a larger image using Pillow?
提问by user1796160
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
What is causing this error? They are both RGB images, the docs don't say anything about this error.
是什么导致了这个错误?它们都是 RGB 图像,文档没有说明此错误。
采纳答案by rayt
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.
问题在于第一次粘贴 - 根据 PIL 文档(http://effbot.org/imagingbook/image.htm),如果没有传递“box”参数,则图像的大小必须匹配。
EDIT: 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))
...
回答by JH_WK
So I might be a little late, but maybe it helps the people coming after a little:
所以我可能来晚了一点,但也许它可以帮助后面的人:
When I had the same issue I couldn't find much about it. So I wrote a snippet which pastes one image into another.
当我遇到同样的问题时,我找不到太多关于它的信息。所以我写了一个片段,将一个图像粘贴到另一个图像中。
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
Not necessarely the best approach, since it takes roughly O(N^2), but it works for small images. Maybe someone can improve the code to be more efficient.
不一定是最好的方法,因为它大约需要 O(N^2),但它适用于小图像。也许有人可以改进代码以提高效率。
I made it in a hurry, so it doesnt have input validation either. just know that the width and height of the source image MUST be smaller or equal to width and height of the target image, otherwise it will crash. You can also only paste the whole image, and not sections or non-rectangular images.
我是匆忙完成的,所以它也没有输入验证。只知道源图像的宽高必须小于或等于目标图像的宽高,否则会崩溃。您也可以只粘贴整个图像,而不能粘贴部分或非矩形图像。