如何用python制作一个水印生成器
Admin 2022-09-17 群英技术资讯 1190 次浏览
本篇内容介绍了“如何用python制作一个水印生成器”的有关知识,在实际项目的操作过程或是学习过程中,不少人都会遇到这样的问题,接下来就让小编带大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!制作一个水印生成器,方便训练水印图片数据采集。
图片水印生成器,可以给指定图片文件或者目录添加水印,水印支持自定义文本、位置、颜色、大小。
# -*- encoding=utf-8 -*-
import time
from PIL import Image, ImageDraw,ImageFont,ImageEnhance
"""图片水印生成器,自定义文字,颜色,大小,位置"""
# 横向上方水印
def auto_make_watermark1(filepath,content,color,savefilepath):
"""
:param filepath: 图片路径
:param content: 水印文字
:param color: 水印颜色
:param savefilepath: 保存路径
:return:
"""
image = Image.open(filepath).convert('RGB')
draw = ImageDraw.Draw(image)
font=ImageFont.truetype("simsun.ttc", 40, encoding="unic",index=1) # 设置水印字体
draw.text((80, 200), content,color, font) # 设置水印位置
image.save(savefilepath)
# 横向中间水印
def auto_make_watermark2(filepath,content,color,savefilepath):
"""
:param filepath: 图片路径
:param content: 水印文字
:param color: 水印颜色
:param savefilepath: 保存路径
:return:
"""
image = Image.open(filepath).convert('RGB')
draw = ImageDraw.Draw(image)
font=ImageFont.truetype("simsun.ttc", 40, encoding="unic",index=1) # 设置水印字体
draw.text((80, 400), content,color, font) # 设置水印位置
image.save(savefilepath)
# 横向最下方水印
def auto_make_watermark3(filepath,content,color,savefilepath):
"""
:param filepath: 图片路径
:param content: 水印文字
:param color: 水印颜色
:param savefilepath: 保存路径
:return:
"""
image = Image.open(filepath).convert('RGB')
draw = ImageDraw.Draw(image)
font=ImageFont.truetype("simsun.ttc", 40, encoding="unic",index=1) # 设置字体
draw.text((80,image.size[1]-150), content,color, font) # 设置水印位置
image.save(savefilepath)
# 横向中间倾斜水印45度
def auto_make_watermark4(filepath,content,color,savefilepath,radio):
"""
:param filepath: 图片路径
:param content: 水印文字
:param color: 水印颜色
:param savefilepath: 保存路径
:param radio: 水印倾斜角度
:return:
"""
im = Image.open(filepath)
watermark = Image.new('RGBA', im.size)
draw = ImageDraw.Draw(watermark, 'RGBA')
font = ImageFont.truetype("simsun.ttc", 40, encoding="unic", index=1)
# x y 坐标
draw.text((80, 400), content, font=font, fill=color)
# 旋转45度
watermark = watermark.rotate(radio, Image.BICUBIC)
# 透明的
alpha = watermark.split()[3]
alpha = ImageEnhance.Brightness(alpha).enhance(0.7)
watermark.putalpha(alpha)
# 合成新的图片
image2 = Image.composite(watermark, im, watermark)
image2.save(savefilepath)
if __name__ == '__main__':
time1=time.time()
filepath=r'F:/img_spam/python添加水印/10064003738101263000320010013284.jpg'
savefilepath='F:/img_spam/python添加水印/生成/'
text="诚招发单人,vx:18668124728"
colors=['red','green','black']
for color in colors:
auto_make_watermark1(filepath,text,color,savefilepath+'10064003738101263000320010013285'+'_'+color+'_'+'top'+'.jpg')
auto_make_watermark2(filepath, text, color,savefilepath + '10064003738101263000320010013285' + '_' + color+'_' +'mmddle'+'.jpg')
auto_make_watermark3(filepath, text, color,savefilepath + '10064003738101263000320010013285' + '_' + color +'_'+ 'down' + '.jpg')
auto_make_watermark4(filepath, text, color,savefilepath + '10064003738101263000320010013285' + '_' + color+'_' + 'angle' + '.png',45)
auto_make_watermark4(filepath, text, color,savefilepath + '10064003738101263000320010013285' + '_' + color + '_' +'angle_50' + '.png', -50)
time2=time.time()
print('总共耗时:' + str(time2 - time1) + 's')

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要为大家介绍了Python内建类型bytes的深入理解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
今天介绍一下Python中常用的正则表达式处理函数。Python的正则表达式主要有两种方法完成模式匹配:『搜索』和『匹配』 re match re mat
这篇文章主要给大家介绍dataframe数据选择的内容,有时候我们需要对数据进行筛选,选择我们想要的数据,下面给给大家分享使用dataframe数据选择的方法,小编觉得是比较实用的,感兴趣的朋友就往下看吧。
这篇文章主要介绍了Django实现文件分享系统,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
在进行数据分析时经常需要按照一定条件创建新的数据列,然后进行进一步分析,下面这篇文章主要给大家介绍了关于Python Pandas多种添加行列数据方法的相关资料,需要的朋友可以参考下
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008