-
Notifications
You must be signed in to change notification settings - Fork 0
/
watermark.py
70 lines (52 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
from PIL import ImageDraw, Image, ImageFont, ImageOps
def find_font_size(text, font, image, target_width_ratio):
tested_font_size = 1
tested_font = ImageFont.truetype(font, tested_font_size)
observed_width, observed_height = get_text_size(text, image, tested_font)
estimated_font_size = tested_font_size / (observed_width / image.width) * target_width_ratio
return round(estimated_font_size)
def get_text_size(text, image, font):
im = Image.new('RGB', (image.width, image.height))
draw = ImageDraw.Draw(im)
return draw.textsize(text, font)
def make_text():
key = True
while key:
try:
interrupt = input('Press "c/continue" to interrupt: ')
if interrupt.lower() == 'c' or interrupt.lower() == 'continue':
text = input('Input single or multi line text: ')
text_size = int(input('Input text size from 0 till 100: '))
font = input('Input text font: ')
rotation = int(input('Input rotation in degrees: '))
opacity = int(input('Input opacity for text from 0 till 255, transparent and not transparent respectively:'))
position = int(input('Input x: ')), int(input('Input y: '))
yield text, text_size, font, rotation, opacity, position
else:
break
except:
break
def make_canvas():
size = int(input('Input width of canvas: ')), int(input('Height of canvas: '))
background_colour = int(input('Input background colour first: ')), int(input('\rsecond:')), int(input('\rthird: ')), int(input('\rInput transparency from 0 to 255, absolutely transparent and not transparent respectively: '))
return size, background_colour
def make_watermark():
canvas = make_canvas()
image = Image.new('RGBA', size=canvas[0], color=canvas[1])
for one in make_text():
text_ = one[0]
text_size = int(one[1]) / 100
font_family = one[2]
angle = one[3]
text_opacity = one[4]
position = one[5]
font_size = find_font_size(text_, font_family, image, text_size)
font = ImageFont.truetype(font_family, font_size)
text_layer = Image.new('L', canvas[0])
draw = ImageDraw.Draw(text_layer)
draw.text(position, text_, font=font, fill=text_opacity)
rotated_text_layer = text_layer.rotate(angle, expand=0)
image.paste(ImageOps.colorize(rotated_text_layer, (0, 0, 0), (0, 0, 0)), (0, 0), rotated_text_layer)
image.save('WaterMark.png')
image.show()
make_watermark()