use*_*412 3 python png transparency python-imaging-library
我有一堆图像,需要在其上放置文本叠加层。我使用 GIMP(具有透明度的 PNG)创建了叠加层,并尝试将其粘贴到其他图像的顶部:
from PIL import Image
background = Image.open("hahn_echo_1.png")
foreground = Image.open("overlay_step_3.png")
background.paste(foreground, (0, 0), foreground)
background.save("abc.png")
Run Code Online (Sandbox Code Playgroud)
然而,我得到的不是在顶部显示漂亮的黑色文本,而是:
overlay.png 在 Gimp 中看起来像这样:
所以我希望看到一些漂亮的黑色文本,而不是这种五颜六色的混乱。
有任何想法吗?我缺少一些 PIL 选项吗?
如上所述vrs,使用alpha_composite这样的答案:How to merge a transparent png image with another image using PIL
就可以了。确保图像处于正确的模式 (RGBA)。
完整的解决方案:
from PIL import Image
background = Image.open("hahn_echo_1.png").convert("RGBA")
foreground = Image.open("overlay_step_3.png").convert("RGBA")
print(background.mode)
print(foreground.mode)
Image.alpha_composite(background, foreground).save("abc.png")
Run Code Online (Sandbox Code Playgroud)
结果:
| 归档时间: |
|
| 查看次数: |
1604 次 |
| 最近记录: |