枕形水印图像

2024-10-01 19:20:03 发布

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

我正在尝试水印的图像,但我不能这样做,没有粘贴水印的背景色。(参见下面的示例)

我试图从下面的website实现教程

从下图可以看出,当前结果显示的水印背景为黑色,这是不可取的。(有关所需结果,请参见预期结果图像)

我正在使用Linux/Ubuntu20.04和Python3.8

内容如下:

背景图像

enter image description here

水印图像

enter image description here

当前结果

enter image description here

预期结果

enter image description here

使水印透明后的当前结果

enter image description here

下面是我使用的代码,和网站教程示例相同

#adding transparency to watermark image(as suggested)
from PIL import Image, ImageFilter
img = Image.open('path_to_watermark').convert('RGBA')
img.putalpha(130) 
img.save('path_to_watermark')


def watermark_with_transparency(input_image_path, output_image_path, watermark_image_path, position):
    base_image = Image.open(input_image_path).convert('RGBA')
    watermark = Image.open(watermark_image_path).convert('RGBA')
    width, height = base_image.size
    transparent = Image.new('RGBA', (width, height), (0,0,0,0))
    transparent.paste(base_image, (0,0))
    transparent.paste(watermark, position, mask=watermark)
    transparent.show()
    transparent.save(output_image_path)

if __name__ == '__main__':
    img = 'path_to_input_image /home/...'
    watermark_with_transparency(img, 'path_to_output', 'path_to_watermark', position=(0,0))

我做错了什么?谢谢你的帮助


Tags: topath图像imageconvertimginputoutput
1条回答
网友
1楼 · 发布于 2024-10-01 19:20:03

您正在粘贴一个没有透明度的图像。 作为PNG,并不意味着它包含透明度,而是一个alpha层,在这种情况下,每个像素等于255

不需要使用putalpha使其透明,只需要一个黑色区域透明的PNG文件

相关问题 更多 >

    热门问题