-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoverlay_whole.py
164 lines (127 loc) · 4.69 KB
/
overlay_whole.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#Copyright (c) 2015 Crowd Dynamics Labs
#
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all
#copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#SOFTWARE.
from PIL import Image, ImageDraw, ImageFont, ImageFilter
from random import randint
from scipy import ndimage
import numpy
hex_black = '%02x%02x%02x' % (0,0,0)
hex_black = int(hex_black, 16)
hex_white = '%02x%02x%02x' % (255,255,255)
hex_white = int(hex_white, 16)
# overlays text on an image
def draw_text(text, color, font_size, base, coordinates):
# make a blank image for the text, initialized to transparent text color
txt = Image.new('RGBA', base.size, (255,255,255,0))
# get a font
fnt = ImageFont.truetype('/System/Library/Fonts/Helvetica.dfont', font_size)
# get a drawing context
d = ImageDraw.Draw(txt)
d.text(coordinates, text, font=fnt, fill=color+(255,))
out = Image.alpha_composite(base, txt)
return out
# returns the width and height in pixels of the text
def text_size(text, font_size):
# make a blank image for the text, initialized to transparent text color
txt = Image.new('RGBA', (font_size*len(text), font_size*len(text)) , (255,255,255,0))
# get a font
fnt = ImageFont.truetype('/System/Library/Fonts/Helvetica.dfont', font_size)
# get a drawing context
d = ImageDraw.Draw(txt)
d.text((0,0), text, font=fnt, fill=(0,0,0,255))
text_size = (d.textsize(text, fnt))
return text_size
def overlay_whole_begin(text,filename):
#text = "20% of your friends eat at chipotle"
font_size = 200
random_num = randint(1,20)
img_file = '/Local/Users/dev/NetworkLabs/topgenerator/data/img/food/'+str(random_num)+'.jpg'
base = Image.open(img_file).convert('RGBA')
base_w, base_h = base.size
# remove 10 border
base_w = base_w - 20
base_h = base_h - 20
# overlay dark canvas
canvas = Image.new("RGBA", base.size, (0,0,0,128))
combined = Image.alpha_composite(base, canvas)
original = combined
words = text.split(' ')
words = [word+' ' for word in words]
# find text size and store in list
size_list = [] # list of word sizes
for word in words:
text_dim = text_size(word, font_size)
size_list.append(text_dim)
done = False
new_size_list = []
while not done:
has_new_font_size = False
x = 10
y = 10
for i in range(len(size_list)):
print(words[i])
w = size_list[i][0]
h = size_list[i][1]
# if word is longer than image width, decrease font size
# until word can fit in width and reset the process
while(w > base_w):
print("word too long ", w, base_w)
font_size = font_size - 1
has_new_font_size = True
new_size_list = []
for word in words:
text_dim = text_size(word, font_size)
new_size_list.append(text_dim)
size_list = new_size_list
combined = original
break
# checks if current word fits in this row
if x+w > base_w-10:
# exceeds row width, go to next row by incrementing y by the height of the previous word
print("exceeds row width")
print(x, y, w, h)
y = y+size_list[i-1][1]
x = 10
# draw text
combined = draw_text(words[i], (256,256,256), font_size, combined, (x, y))
x = x+w
# check whether the current word overflows the height of the image
# if it does, decrease font size by 1 and try again
if(y+h > base_h-10):
print("height exceeds")
font_size = font_size - 1
has_new_font_size = True
new_size_list = []
for word in words:
text_dim = text_size(word, font_size)
new_size_list.append(text_dim)
size_list = new_size_list
combined = original
break
else:
# word does not exceed current row width
print("does not exceed. x = ", x)
combined = draw_text(words[i], (256,256,256), font_size, combined, (x, y))
x = x+w
if i==len(size_list)-1:
done = True
combined.save(filename)
return
if __name__ == "__main__":
overlay_whole_begin()