如何在Python中更改图像的不透明度并与另一个图像合并

2024-05-12 01:22:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在研究如何把两张照片放在一起,把最上面的一张透明度提高到50%左右。在

到目前为止,我找到了这个:

from PIL import Image

def merge():
    background = Image.open("ib.jpg")
    background = background .convert('L') #only foreground color matters
    foreground = Image.open("if.jpg")

    background.paste(foreground, (0, 0), foreground)
    background.show()

但它只输出一个空白图像。在

两种尺寸都一样。在

在日立银行公司名称:

ib.jpg

在如果.jpg公司名称:

if.jpg

期望输出:

enter image description here

对于使用RGB或RGBA文件执行此操作有什么提示吗?我应该处理这两种类型(有些,事实上,有alpha层)。在

谢谢


Tags: fromimageimport名称pildef公司open
1条回答
网友
1楼 · 发布于 2024-05-12 01:22:34

必须使用PIL.Image中的blend函数:

from PIL import Image
bg = Image.open("1.jpg")
fg = Image.open("2.jpg")
# set alpha to .7
Image.blend(bg, fg, .7).save("out.png")

enter image description here

相关问题 更多 >