-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatermarking.py
94 lines (80 loc) · 2.94 KB
/
watermarking.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/home/battleman/Programs/anaconda3/bin/python
# -*- coding: utf-8 -*-
"""
This module allows to easily watermark an image with a given text.
This is useful to credit the website/creator of an image
Credits to http://www.thecodingcouple.com/watermark-images-python-pillow-pil/
"""
from PIL import Image, ImageDraw, ImageFont
def add_watermark(image, text, screen_size):
"""
Add a watermark to an image
Positional Arguments:
input: absolute path to the image
text: text to display
output: absolute path to the output image
"""
image = image.convert('RGBA')
screen_height = screen_size[1]
margin = 5
text_color = "#BBB"
text_canvas = Image.new('RGBA',
(image.height, image.height),
(0, 0, 0, 170))
text_draw = ImageDraw.Draw(text_canvas)
font = ImageFont.truetype('FreeMono', 15)
text_width, text_height = text_draw.textsize(text, font)
x_coord = (image.height-text_width)//2 # width - textWidth - margin
y_coord = text_height//4 # height - textHeight - margin
text_draw.text((x_coord, y_coord), text, text_color, font)
text_canvas = text_canvas.rotate(90)
watermark_canvas = Image.new('RGBA',
(text_height+2*margin, text_width+2*margin),
(0, 0, 0, 0))
watermark_canvas.paste(
text_canvas,
(0, -(image.height-text_width)//2 + margin))
image.paste(watermark_canvas,
(margin, (screen_height-text_width)//2),
watermark_canvas)
return image.convert("RGB")
def resize_image(image, screen_size=(1920, 1080)):
"""
Takes an image and new dimensions, and return the image resized
accordingly. If one of the dimensions is None, it will be resized to
keep current proportions
"""
width, height = screen_size
image_width, image_height = image.size
if image_width/image_height > 16/9:
# Too tall
image_height = (image_height * width) // image_width
image_width = width
elif image_width/image_height < 16/9:
# too wide
image_width = (image_width * height) // image_height
image_height = height
else:
# good ratio
image_width = width
image_height = height
resized = image.resize(
(int(image_width), int(image_height)), Image.ANTIALIAS)
canvas = Image.new("RGB", screen_size)
canvas.paste(resized, (
(width-image_width)//2 if width != image_width else 0,
(height-image_height)//2 if height != image_height else 0))
return canvas
def test():
"""
This module is here to present an example of use
"""
image = Image.open("image.jpeg")
resized = resize_image(image)
watermarked = add_watermark(
resized,
"That is a watermark, probably not the best tho",
(1920, 1080))
watermarked.save('output.jpeg')
if __name__ == "__main__":
test()