Python PIL.Image.alpha_复合-选择起点

Python PIL.Image.alpha_复合-选择起点,python,python-imaging-library,Python,Python Imaging Library,我需要“粘贴”一个0阿尔法层的图像到另一个。为此,我使用PIL.Image.alpha_复合函数。它的文档说,两张图片的大小应该相同。但这绝对不是真的。这段代码显示,我可以混合两个不同大小的图像: from PIL import Image, ImageDraw image_size = (700, 500) rect_size = (700, 200) shape = [(0, 0), rect_size] #Create blank image 700x500 im1 = Image.

我需要“粘贴”一个0阿尔法层的图像到另一个。为此,我使用PIL.Image.alpha_复合函数。它的文档说,两张图片的大小应该相同。但这绝对不是真的。这段代码显示,我可以混合两个不同大小的图像:

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()
结果可能是:


我的问题是我想把矩形放在底部。有可能吗?

为什么不将im2大小设置为图像大小(700500),并在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()

因为我在
rect_size
区域内绘制文本对象,然后我想将其放入基础图像中。在这里,使用rect的示例更简单。