添加水印

def draw():
#得到一个图片
with Image.open("images/dajidali.jpeg").convert("RGBA") as base:
# make a blank image for the text, initialized to transparent text color
#制作一个空图片, 大小和基本突一样 color是一个元组,前三个的规则和RGB相同,最后一个代表透明度,范围在0-255之间,0全透,255全不透;
txt = Image.new("RGBA", base.size, color=(255,255,200,100))
# get a font 获取字体 和 字体大小
fnt = ImageFont.truetype("fonts/HuaGuangGangTieZhiHei-KeBianTi-2.ttf", size=200)
# get a drawing context 获得一个可绘画对象
d = ImageDraw.Draw(txt)
# draw text, half opacity text((x,y)) 默认的是从左上角(0,0) x=横,y竖; text= 要书写的汉字; fill=填充的颜色(255, 255, 255, 200)
d.text(xy=(0, 0), text="Hello", font=fnt, fill=(122,122,122,50))
# draw text, full opacity
d.text(xy=(500, 0), text="World", font=fnt, fill=(255, 255, 255, 50))
txt.show()
out = Image.alpha_composite(base,txt)
out.show()
posted on 2022-08-04 17:10  《=》  阅读(67)  评论(0)    收藏  举报