首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PIL.Image.alpha_composite -选择起点

PIL.Image.alpha_composite -选择起点
EN

Stack Overflow用户
提问于 2020-03-01 02:57:05
回答 1查看 198关注 0票数 0

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

代码语言:javascript
复制
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()

结果可能是这样的:

我的问题是我想把矩形放在底部。有没有可能以某种方式这样做?

EN

回答 1

Stack Overflow用户

发布于 2020-03-01 03:35:45

为什么不将im2大小设置为图像大小(700,500)并在im2的底部绘制矩形。如下所示:

代码语言:javascript
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60468547

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档