如何在普通图像上粘贴半透明图像并获得透明度 PIL?
How to paste a semi-transparent image on top of a normal image and get the transparency 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')
所以我想在普通图像上粘贴透明图像。但是由于某种原因,透明度消失了。
有没有办法在不丢失第二张图片透明度的情况下,将第二张图片(透明图片)的透明度保持在第一张图片(普通图片)之上?
(顺便说一句,我是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')