由于我用的是GitHub图层,配合utools的图床工具上传,但没有加水印功能,于是打算利用github action自动加水印。
代码如下:
from PIL import Image, ImageDraw, ImageFont
img = Image.open("test.png") text = "御驾亲征"
font = ImageFont.truetype("Shouxieti.ttf", 75)
layer = img.convert("RGBA")
text_layer = Image.new("RGBA", (img.size[0]*2,img.size[1]*2), (255, 255, 255, 0)) Image_draw = ImageDraw.Draw(text_layer)
textsize_x, textsize_y = Image_draw.textsize(text, font=font) nums = max(round(layer.size[0] / textsize_x), round(layer.size[1] / textsize_y)) print(nums) for i in range (1,nums): for j in range(1,nums): text_xy = (textsize_x*(i-1)*1.5,textsize_y*(j-1)*2.5) Image_draw.text(text_xy, text, font=font, fill=(255, 255, 255, 50))
text_layer = text_layer.rotate(45) text_layer = text_layer.crop((text_layer.size[0]/2 - img.size[0]/2, text_layer.size[1]/2 - img.size[1]/2, text_layer.size[0]/2 + img.size[0]/2, text_layer.size[1]/2 + img.size[1]/2))
after = Image.alpha_composite(layer, text_layer) after.save("output.png")
|
效果看图吧,后面再出一篇利用github action自动加水印。