示例

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
# -*- coding:utf-8 -*-

from PIL import Image, ImageDraw, ImageFont
import os


def add_text(img_path, text='svinda.github.io', show_result_image=False):
img = Image.open(img_path)
draw = ImageDraw.Draw(img)
font = ImageFont.truetype('C:/windows/fonts/Arial.ttf', size=20)
# color = (255, 222, 111)
width, height = img.size
draw.text((width - 150, height - 55), text, (14, 222, 111))

# 添加透明度
draw.text((width - 150, height - 35), text, (14, 222, 111, 0))

# 添加字体
draw.text((width - 150, height - 75), text, (14, 222, 111), font=font)
ImageDraw.Draw(img)

(path, suffix) = os.path.splitext(img_path)
print(path, suffix)
img.save(path + '_watermark.png')
if show_result_image:
## (阻塞)打开添加水印后的图片
img.show()


# 多张图片添加同一水印
def add_text_all(img_paths=[], text='svinda.github.io', show_result_image=False):
for img_path in img_paths:
add_text(img_path, text, show_result_image)


if __name__ == '__main__':
image_path = r"images\hexo-文章插入图片.png"
add_text(image_path, 'svinda.github.io')

add_text_all([r"images\hexo-文章插入图片.png", r"images\hexo-文章插入图片 - 副本.png"])

效果图
插入图片_watermark