应业务需求使用python开发图片批量添加水印工具
import tkinter
import argparse
import os
import sys
import math
import random
import glob
from PIL import Image, ImageFont, ImageDraw, ImageEnhance, ImageChops
TTF_FONT = u'./font/bird.ttf'
def add_mark(imagePath, mark, args,resultpath):
'''
添加水印,然后保存图片
'''
im = Image.open(imagePath)
image = mark(im)
if image:
name = os.path.basename(imagePath)
if not os.path.exists(args):
os.mkdir(args)
new_name = resultpath+"/pic_"+name
if os.path.splitext(new_name)[1] != '.png':
image = image.convert('RGB')
image.save(new_name)
print(name + " Success.")
else:
print(name + " Failed.")
def gen_mark(marks,color):
'''
生成mark图片,返回添加水印的函数
'''
# 字体宽度
width = len(marks) * 50
# 创建水印图片(宽度、高度)
mark = Image.new(mode='RGBA', size=(width, 50))
# 生成文字
draw_table = ImageDraw.Draw(im=mark)
draw_table.text(xy=(0, 0),
text=marks,
fill=color,
font=ImageFont.truetype(TTF_FONT,
size=50))
del draw_table
# 裁剪空白
mark = crop_image(mark)
# 透明度
set_opacity(mark, 0.15)
def mark_im(im):
''' 在im图片上添加水印 im为打开的原图'''
# 计算斜边长度
c = int(math.sqrt(im.size[0]*im.size[0] + im.size[1]*im.size[1]))
# 以斜边长度为宽高创建大图(旋转后大图才足以覆盖原图)
mark2 = Image.new(mode='RGBA', size=(c, c))
# 在大图上生成水印文字,此处mark为上面生成的水印图片
y, idx = 0, 0
while y < c:
# 制造x坐标错位
x = -int((mark.size[0] + 75)*0.5*idx)
idx = (idx + 1) % 2
while x < c:
# 在该位置粘贴mark水印图片
mark2.paste(mark, (x, y))
x = x + mark.size[0] + 75
y = y + mark.size[1] +75
# 将大图旋转一定角度
mark2 = mark2.rotate(30)
# 在原图上添加大图水印
if im.mode != 'RGBA':
im = im.convert('RGBA')
im.paste(mark2, # 大图
(int((im.size[0]-c)/2), int((im.size[1]-c)/2)), # 坐标
mask=mark2.split()[3])
del mark2
return im
return mark_im
def crop_image(im):
'''裁剪图片边缘空白'''
bg = Image.new(mode='RGBA', size=im.size)
diff = ImageChops.difference(im, bg)
del bg
bbox = diff.getbbox()
if bbox:
return im.crop(bbox)
return im
def set_opacity(im, opacity):
'''
设置水印透明度
'''
assert opacity >= 0 and opacity <= 1
alpha = im.split()[3]
alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
im.putalpha(alpha)
return im
window=tkinter.Tk()
window.title('水印批量添加工具--WuHao') #title
window.geometry('730x220')
window.resizable(0,0)
lable = tkinter.Label(window,text = "文件路径:",width=10, height=2)
lable.place(x = 10,y = 20,width = 100,height = 20)
lable1 = tkinter.Label(window,text = "保存路径:",width=10, height=2)
lable1.place(x = 10,y = 60,width = 100,height = 20)
lable2 = tkinter.Label(window,text = "水印文字:",width=10, height=2)
lable2.place(x = 10,y = 100,width = 100,height = 20)
lable3 = tkinter.Label(window,text = "水印颜色:",width=10, height=2)
lable3.place(x = 10,y = 140,width = 100,height = 20)
input = tkinter.Entry(window)
input.place(x = 100,y = 20,width = 300,height = 20)
input1 = tkinter.Entry(window)
input1.place(x = 100,y = 60,width = 300,height = 20)
input2 = tkinter.Entry(window)
input2.place(x = 100,y = 100,width = 300,height = 20)
input3 = tkinter.Entry(window)
input3.place(x = 100,y = 140,width = 300,height = 20)
txtlable = tkinter.Text(window, width=80, heigh=160, bg='#f6f8fa') # 宽度为80个字母(40个汉字),高度为1个行高
txtlable.place(x = 420,y = 20,width = 300,height = 190)
def dealSrc(path,resultpath,txt,colortxt):
mark = gen_mark(txt, colortxt)
txtlable.insert('end', "开始读取.....")
for pidImage in glob.glob(path + "/*.[jp][pn]g"):
add_mark(pidImage, mark, path,resultpath)
txtlable.insert('end', "\n" + pidImage + "\tSuccess")
txtlable.insert('end', "\n处理成功!")
def show():
txtlable.delete(1.0, tkinter.END)
dealSrc(input.get(),input1.get(),input2.get(),"#"+input3.get())
theButton1 = tkinter.Button(window, text = "确认",width = 10,command = show)
theButton1.place(x = 100,y = 180,width = 300,height = 30)
window.mainloop()
源码及发行版下载:https://download.csdn.net/download/weixin_37545129/12816658