-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.py
116 lines (97 loc) · 4.44 KB
/
image.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from PIL import Image, ImageDraw, ImageFont
import os
# DEAFULTS
INPUT = "input/poster.png"
OUTPUT = "output/"
POSITION='bcl'
DISPLAY = False
TEXT = "" # Let it be empty unless you want text and watermark both
FONT = "fonts/Helvetica.ttf"
COLOR = "white"
TEXT_SIZE = 40
TEXT_POSITION = "bc"
TEXT_ALIGN_WATERMARK = "r" # Text Position wrt Watermark
def watermark(
input_image_path,
output_image_path,
watermark_image_path,
position_logo,
text=TEXT,
color=COLOR,
text_size=TEXT_SIZE,
text_position=TEXT_ALIGN_WATERMARK
):
base_image = Image.open(input_image_path)
watermark = Image.open(watermark_image_path).convert('RGBA')
SIZE = 300,150
watermark.thumbnail((SIZE),resample=0)
width_base, height_base = base_image.size
width_water, height_water = watermark.size
transparent = Image.new('RGB', (width_base, height_base))
transparent.paste(base_image, (0,0))
bias_width=int(width_base*.03)
bias_height=int(height_base*.03)
mapper_logo_position={'tl':(bias_width,bias_height),
'tc':(width_base//2-width_water//2,bias_height),
'tr':(width_base-width_water-bias_width,bias_height),
'cl':(bias_width,height_base//2-height_water),
'cr':(width_base-width_water-bias_width,height_base//2-height_water),
'c':(width_base//2-width_water//2,height_base//2-height_water),
'bl':(bias_width,height_base-height_water-bias_height), #
'bc':(width_base//2-width_water//2,height_base-height_water-bias_height),
'bcl':(int(width_base/2.2)-width_water//2,height_base-height_water-bias_height),
'br':(width_base-width_water-bias_width,height_base-height_water-bias_height)}
transparent.paste(watermark, mapper_logo_position[position_logo], mask=watermark)
drawing = ImageDraw.Draw(transparent)
#drawing = Image.new('RGB',transparent.size,(255,255,255,255))
font = ImageFont.truetype(FONT, text_size)
textwidth, textheight = drawing.textsize(text, font)
mapper_text_position={
'r':(mapper_logo_position[position_logo][0]+width_water+bias_width//2,mapper_logo_position[position_logo][1]+height_water//2-textheight//2),
'b':[mapper_logo_position[position_logo][0]+width_water//2-textwidth//2,mapper_logo_position[position_logo][1]+height_water+bias_width//2]
}
position_text=mapper_text_position[text_position]
if(textwidth<width_water):
mapper_text_position['b'][0]=mapper_logo_position[position_logo][0]+(width_water-textwidth)//2
drawing.text(position_text, text, fill=color,font=font)
if DISPLAY:
transparent.show()
transparent.save(output_image_path)
def watermark_with_text(input_image_path,
output_image_path,
text=TEXT,
text_position=TEXT_POSITION,
color=COLOR,
text_size=TEXT_SIZE,
):
base_image = Image.open(input_image_path)
width_base, height_base = base_image.size
drawing = ImageDraw.Draw(base_image)
bias_width=int(width_base*.05)
bias_height=int(height_base*.05)
if bias_height>50:
bias_height=50
if bias_width>50:
bias_width=50
font = ImageFont.truetype(FONT, text_size)
textwidth, textheight = drawing.textsize(text, font)
mapper_position={'tl':(bias_width,bias_height),
'tc':(width_base//2-textwidth//2,bias_height),
'tr':(width_base-textwidth-bias_width,bias_height),
'cl':(bias_width,height_base//2-textheight),
'cr':(width_base-textwidth-bias_width,height_base//2-textheight),
'c':(width_base//2-textwidth//2,height_base//2-textheight),
'bl':(bias_width,height_base-textheight-bias_height),
'bc':(width_base//2-textwidth//2,height_base-textheight-bias_height),
'br':(width_base-textwidth-bias_width,height_base-textheight-bias_height)}
drawing.text(mapper_position[text_position], text,fill=color, font=font)
if DISPLAY:
base_image.show()
base_image.save(output_image_path)
def watermark_a_folder(folder_path):
global OUTPUT
logo_list=os.listdir(folder_path)
for i in logo_list:
OUTPUT=OUTPUT+'result'+'_'+i
watermark(INPUT,OUTPUT,folder_path+i,POSITION)
OUTPUT = "output/"