脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - Python - python实现水印生成器

python实现水印生成器

2022-12-20 12:48东华果汁哥 Python

这篇文章主要为大家详细介绍了python实现水印生成器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了python实现水印生成器,python制作图片水印的具体代码,供大家参考,具体内容如下

制作一个水印生成器,方便训练水印图片数据采集。
图片水印生成器,可以给指定图片文件或者目录添加水印,水印支持自定义文本、位置、颜色、大小。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# -*- 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')

python实现水印生成器

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/u013421629/article/details/90704354

延伸 · 阅读

精彩推荐