图片批量添加水印
批量给指定文件夹内图片添加水印,并且能提取图片本身信息或指定信息进行添加,字体大小适配图片
减少简单的ps操作,同时使文字显现出蓝白的立体效果
from PIL import Image, ImageDraw, ImageFont
import glob
import re
files = glob.glob('./*.jpg')
for file in files:
img = Image.open(file)
p = re.compile('[\u4e00-\u9fa5]+')#匹配汉字
width = img.size[0]
height = img.size[1]
myfont = ImageFont.truetype('arial.ttf', width//30+2)
myfont2 = ImageFont.truetype('arial.ttf', width // 30)
myfont3 = ImageFont.truetype('arial.ttf', width // 30+1)
draw = ImageDraw.Draw(img)
text = "XXX"
text_width = myfont3.getsize(text)
text_width2 = myfont2.getsize(text)
ext_coordinate = int((width-text_width[0])/2), int(height-text_width[1])
ext_coordinate2 = int((width - text_width2[0]) / 2), int(height - text_width[1])
draw.text(ext_coordinate, text, fill=(0, 138, 184), font=myfont3) #蓝色
draw.text(ext_coordinate2, text, fill=(255, 255, 255), font=myfont2)
str = "XXX:"+p.search(file).group()#读取汉字
text_width = myfont.getsize(str)
text_width2 = myfont2.getsize(str)
ext_coordinate = int((width-text_width[0])/2), int(height-text_width[1]*2)
ext_coordinate2 = int((width - text_width2[0]) / 2), int(height - text_width[1] * 2)
draw.text(ext_coordinate,str,fill=(0,138,184),font=myfont)
draw.text(ext_coordinate2, str, fill=(255, 255, 255), font=myfont2)
text = "XXXX"
text_width = myfont.getsize(text)
text_width2 = myfont2.getsize(text)
ext_coordinate = int((width-text_width[0])/2), int(height-text_width[1]*3)
ext_coordinate2 = int((width - text_width2[0]) / 2), int(height - text_width[1] * 3)
draw.text(ext_coordinate,text,fill=(0,138,184),font=myfont)
draw.text(ext_coordinate2, text, fill=(255, 255, 255), font=myfont2)
img.show()
#img.save(file)