Python 的 wand 库有一个 Image.watermark 方法,可以简化常见的水印操作。
from wand.image import Image
with Image(filename='background.jpg') as background:
with Image(filename='watermark.png') as watermark:
background.watermark(image=watermark, transparency=0.75)
background.save(filename='result.jpg')
我所在的地方没有安装python,但它应该是这样的。
import Image
photo = Image.open("photo.jpg")
watermark = Image.open("watermark.png")
photo.paste(watermark, (0, 0), watermark)
photo.save("photo_with_watermark.jpg")
您可以使用 pyvips 执行此操作,如下所示:
#!/usr/bin/python3
import sys
import pyvips
image = pyvips.Image.new_from_file(sys.argv[1], access="sequential")
text = pyvips.Image.text(sys.argv[3], dpi=700, rgba=True)
# scale the alpha down to make the text semi-transparent
text = (text * [1, 1, 1, 0.3]).cast("uchar")
# composite in the centre
image = image.composite(text, "over",
x=int((image.width - text.width) / 2),
y=int((image.height - text.height) / 2))
image.write_to_file(sys.argv[2])
然后运行(例如):
$ ./watermark.py PNG_transparency_demonstration.png x.png "Hello world!"
制作: