MCPcopy Create free account
hub / github.com/Royalvice/DocDiff / gen_mark

Function gen_mark

utils/marker.py:95–165  ·  view source on GitHub ↗

生成mark图片,返回添加水印的函数

(args)

Source from the content-addressed store, hash-verified

93
94
95def gen_mark(args):
96 """
97 生成mark图片,返回添加水印的函数
98 """
99 # 字体宽度、高度
100 is_height_crop_float = '.' in args.font_height_crop # not good but work
101 width = len(args.mark) * args.size
102 if is_height_crop_float:
103 height = round(args.size * float(args.font_height_crop))
104 else:
105 height = int(args.font_height_crop)
106
107 # 创建水印图片(宽度、高度)
108 mark = Image.new(mode='RGBA', size=(width, height))
109
110 # 生成文字
111 draw_table = ImageDraw.Draw(im=mark)
112 draw_table.text(xy=(0, 0),
113 text=args.mark,
114 fill=args.color,
115 font=ImageFont.truetype(args.font_family,
116 size=args.size))
117 del draw_table
118
119 # 裁剪空白
120 mark = crop_image(mark)
121
122 # 透明度
123 set_opacity(mark, args.opacity)
124
125 def mark_im(im):
126 """ 在im图片上添加水印 im为打开的原图"""
127
128 # 计算斜边长度
129 c = int(math.sqrt(im.size[0] * im.size[0] + im.size[1] * im.size[1]))
130
131 # 以斜边长度为宽高创建大图(旋转后大图才足以覆盖原图)
132 mark2 = Image.new(mode='RGBA', size=(c, c))
133
134 # 在大图上生成水印文字,此处mark为上面生成的水印图片
135 y, idx = 0, 0
136 while y < c:
137 # 制造x坐标错位
138 x = -int((mark.size[0] + args.space) * 0.5 * idx)
139 idx = (idx + 1) % 2
140
141 while x < c:
142 # 在该位置粘贴mark水印图片
143 mark2.paste(mark, (x, y))
144 x = x + mark.size[0] + args.space
145 y = y + mark.size[1] + args.space
146
147 # 将大图旋转一定角度
148 mark2 = mark2.rotate(args.angle)
149
150 # 在原图上添加大图水印
151 if im.mode != 'RGBA':
152 im = im.convert('RGBA')

Callers 1

mainFunction · 0.85

Calls 2

crop_imageFunction · 0.85
set_opacityFunction · 0.85

Tested by

no test coverage detected