利用python实现多图片加上水印(可变化字体大小,颜色,位置,水印的深浅)
1.水印代码
from PIL import Image, ImageDraw, ImageFont
src="G:/Desktop/csdn/"
src1="G:/Desktop/csdn/加水印/"
def add_text_to_image(image, text):
font = ImageFont.truetype("G:\Desktop\STXINGKA.TTF", 50, encoding="unic")
rgba_image = image.convert('RGBA')
text_overlay = Image.new('RGBA', rgba_image.size, (255, 255, 255, 0))
image_draw = ImageDraw.Draw(text_overlay)
image_draw.text((100, 200), text, font=font, fill=(255,0,0,40))
image_with_text = Image.alpha_composite(rgba_image, text_overlay)
return image_with_text
def xunhuan():
for i in range(1,5):
im_before = Image.open(src + "{}.png".format(i))
im_after = add_text_to_image(im_before, '不点个关注再走吗???')
im_after.show()
im_after.save(src1 + "{}_after.png".format(i))
if __name__=="__main__":
xunhuan()
2.水印运行截图
3.代码需要注意地方
注意:
例如:fill = (200, 10, 10, 0.5)
对于RGBA模式的图片,填充半透明色,alpha位置取值是0-255,你希望50%,应该是用128,不是0.5。
这样就走了吗???
|