python - 如何在普通图像上粘贴半透明图像并获得透明度 PIL?
问题描述
所以我试图在普通图像上粘贴一个透明图像。但是由于某种原因,透明度消失了。
有没有办法将第二张图像(透明图像)的透明度保持在第一张图像(普通图像)之上,而不会失去第二张图像的透明度?
(顺便说一下,我是 PIL 的新手)
BG = Image.open("BGImage.png") # normal image, size is 1920x1080
BG = BG.convert("RGBA")
overlay = Image.open("OverlayImage.png") # transparent image, size is 1920x1080
overlay = overlay.convert("RGBA")
BG = Image.alpha_composite(BG, overlay) # I tried doing this but it didn't work
BG.save("NewImage.png")
解决方案
像这样使用paste
- 在确保您的图像没有被调色后:
from PIL import Image
# Open background and foreground ensuring they are not palette images
bg = Image.open('bg.png').convert('RGB')
fg = Image.open('fg.png')
# Paste foreground onto background and save
bg.paste(fg,mask=fg)
bg.save('result.png')
推荐阅读
- c# - OpenXML:如何将工作表复制到另一个工作簿?
- angular - NGXS 存储插件破坏导航
- python - 使用 Pandas 的“where”函数时出现类型错误
- sql - 使用批量插入将已删除的记录插入回 SQL Server 数据库
- rally - 如何在拉力赛板上的门票说明中设置图标?
- java - 来自 JNA 和 DLL 的无效内存访问错误
- python - 当我用变量替换硬编码值时,Python SQL 查询返回“ORA-00936:缺少表达式”
- time-series - Q:基于 ts 对象创建组合图
- javascript - 使用 jQuery 和 JavaScript 从表单元素将字符串变量传递给 href
- google-api - 无法使用 API 密钥获取 Google Drive 文件父级