-
Notifications
You must be signed in to change notification settings - Fork 2
/
watermark.py
78 lines (57 loc) · 2.54 KB
/
watermark.py
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
import json
import logging
from random import randint
from PIL import ImageDraw, ImageFont
from PIL.Image import Image
class Watermark:
def __init__(self, text: str, size: int, fg_color: str, bg_color: str, randomize_position: bool):
self.text = text
self.size = size
self.fg_color = fg_color
self.bg_color = bg_color
self.randomize_position = randomize_position
def __str__(self):
return f"text='{self.text}',size={self.size},rpos={self.randomize_position}," + \
f"fg='{self.fg_color}',bg='{self.bg_color}'"
def paint(self, img: Image) -> Image:
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("arial.ttf", self.size)
text_width, text_height = draw.textsize(self.text, font)
if self.randomize_position:
text_position = (randint(20, img.width - text_width - 20), randint(10, img.height - text_height - 10))
else:
text_position = ((img.width - text_width) - 20, (img.height - text_height) - 10)
background_position = (text_position[0] - 10, text_position[1] - 5)
background_dimensions = (text_width + 20, text_height + 10)
draw.rectangle((
background_position,
(background_position[0] + background_dimensions[0], background_position[1] + background_dimensions[1])
), fill=self.bg_color)
draw.text(text_position, self.text, font=font, fill=self.fg_color)
return img
class NoWatermark(Watermark):
def __init__(self):
super().__init__("", 0, "", "", False)
def __str__(self):
return "no watermark"
def paint(self, img: Image) -> Image:
return img
NO_WATERMARK = NoWatermark()
def parse_watermark(options_json: str) -> Watermark:
if options_json == "":
return NO_WATERMARK
if not isinstance(options := json.loads(options_json), dict):
raise ValueError("invalid JSON")
if (text := options.get("text", "jpeg.qwer.tz")) == "":
return NO_WATERMARK
size = options.get("size", 18)
fg_color = options.get("foregroundColor", "#facc15")
bg_color = options.get("backgroundColor", "#ff0000")
randomize_position = options.get("randomizePosition", False)
if not isinstance(text, str) \
or not isinstance(size, int) \
or not isinstance(fg_color, str) \
or not isinstance(bg_color, str) \
or not isinstance(randomize_position, bool):
raise ValueError("invalid JSON")
return Watermark(text, size, fg_color, bg_color, randomize_position)