打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
python开发图片批量添加水印工具


版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
版权

应业务需求使用python开发图片批量添加水印工具

import tkinterimport argparseimport osimport sysimport mathimport randomimport globfrom PIL import Image, ImageFont, ImageDraw, ImageEnhance, ImageChopsTTF_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_"+nameif 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, 0while y < c:# 制造x坐标错位x = -int((mark.size[0] + 75)*0.5*idx)idx = (idx + 1) % 2while x < c:# 在该位置粘贴mark水印图片mark2.paste(mark, (x, y))x = x + mark.size[0] + 75y = 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 mark2return imreturn mark_imdef crop_image(im):'''裁剪图片边缘空白'''bg = Image.new(mode='RGBA', size=im.size)diff = ImageChops.difference(im, bg)del bgbbox = diff.getbbox()if bbox:return im.crop(bbox)return imdef set_opacity(im, opacity):'''设置水印透明度'''assert opacity >= 0 and opacity <= 1alpha = im.split()[3]alpha = ImageEnhance.Brightness(alpha).enhance(opacity)im.putalpha(alpha)return imwindow=tkinter.Tk()window.title('水印批量添加工具--WuHao')     #titlewindow.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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
pytest之通过yaml实现数据驱动
利用OpenCV、Python和Ubidots构建行人计数器程序(附代码&解析)
学习Python动态扩展包stevedore | 闫肃的博客
Python 窗体(tkinter)下拉列表框(Combobox)实例
Python小技巧:用类写装饰器的正确方法,一般新手都不知道
大大雷Python零基础课程第七节:函数
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服