我需要“粘贴”一个图像与0阿尔法层到另一个。为此,我使用PIL.Image.alpha_composite函数。它的文档说,这两个图像应该是相同的大小。但这绝对不是真的。这段代码显示,我可以混合2个不同大小的图像:
from PIL import Image, ImageDraw
image_size = (700, 500)
rect_size = (700, 200)
shape = [(0, 0), rect_size]
#Create blank image 700x500
im1 = Image.new("RGBA", image_size)
#Create blank image for rectangle drawing 700x200
im2 = Image.new("RGBA", rect_size)
#Draw rectangle on it with the same 700x200 dims
im3 = ImageDraw.Draw(im2)
im3.rectangle(shape, fill ="#ffff33")
#Composite 2 images of 700x500 and 700x200 sizes
im1.alpha_composite(im2)
im1.show()结果可能是这样的:

我的问题是我想把矩形放在底部。有没有可能以某种方式这样做?
发布于 2020-03-01 03:35:45
为什么不将im2大小设置为图像大小(700,500)并在im2的底部绘制矩形。如下所示:
from PIL import Image, ImageDraw
image_size = (700, 500)
rect_size = (700, 500)
# Draw rectangle from first point (x=0,y=300) to the second point (x =700,y=500)
shape = (0,300,700,500)
#Create blank image 700x500
im1 = Image.new("RGBA", image_size)
#Create blank image for rectangle drawing 700x200
im2 = Image.new("RGBA", rect_size)
#Draw rectangle on it with the same 700x200 dims
im3 = ImageDraw.Draw(im2)
im3.rectangle(shape, fill ="#ffff33")
#Composite 2 images of 700x500 and 700x200 sizes
im1.alpha_composite(im2)
im1.show()https://stackoverflow.com/questions/60468547
复制相似问题