-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_ad.py
executable file
·314 lines (226 loc) · 9.47 KB
/
build_ad.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/env python3
'''combine multiple images to build ads images to be posted on social media'''
__author__ = "Abdurrahman Ghanem"
import sys
import os
from shutil import copyfile
import json
from resources_crawler import download_url
from PIL import Image, ImageFont, ImageDraw
import unicodedata as ud
import arabic_reshaper
from bidi.algorithm import get_display
import datetime
def is_empty_line(line):
if len(line.strip()) == 0:
return True
else:
return False
latin_letters = {}
def is_latin(uchr):
try: return latin_letters[uchr]
except KeyError:
return latin_letters.setdefault(uchr, 'LATIN' in ud.name(uchr))
def only_roman_chars(unistr):
return all(is_latin(uchr) for uchr in unistr if uchr.isalpha())
def fix_arabic_text(text):
if only_roman_chars(text):
return text
return get_display(arabic_reshaper.reshape(text))
def get_max_font_size(font_file, text, fit_size):
font_size = 8
font = ImageFont.truetype(font_file, font_size)
width, height = font.getsize(text)
while width < fit_size[0] and height < fit_size[1]:
font_size += 1
font = ImageFont.truetype(font_file, font_size)
width, height = font.getsize(text)
return ImageFont.truetype(font_file, font_size - 1)
def get_text_centered_position(font_size, fit_size, x , y):
new_x = x + ((fit_size[0] - font_size[0]) / 2)
new_y = y + ((fit_size[1] - font_size[1]) / 2)
return (new_x , new_y)
def load_json(jsonfile):
if os.path.exists(jsonfile):
with open(jsonfile, "r") as jsonf:
resources_dict = json.load(jsonf)
return resources_dict
def read_index(idx_file):
return load_json(idx_file)
def read_descriptor(desc_file):
return load_json(desc_file)
def download_resources(urls):
out_folders = []
for url in urls:
out_folders.append(download_url(url))
return out_folders
def get_urls(urls_file):
urls = []
with open(urls_file, "r") as f:
for url in f:
urls.append(url)
return urls
def open_template(temp_name):
tn = temp_name
if "templates/" not in temp_name:
tn = "templates/" + temp_name
if os.path.exists(tn):
img = Image.open(tn)
img.load()
return img
def open_template_overlay(temp_name):
tn = temp_name
if "templates/" not in temp_name:
tn = "templates/" + temp_name
if os.path.exists(tn):
toimg = Image.open(tn)
toimg.load()
return toimg
def crop_img_center(img , new_width , new_height):
width, height = img.size
if new_width <= width and new_height <= height:
left = (width - new_width) / 2
upper = (height - new_height) / 2
right = left + new_width
lower = upper + new_height
box = (left, upper, right, lower)
return img.crop(box)
else:
return img
def paste_img(template , to_paste_img , x , y):
box = (x, y, x + to_paste_img.size[0], y + to_paste_img.size[1])
if to_paste_img.mode == "RGBA":
template.paste(to_paste_img, box, to_paste_img)
else:
template.paste(to_paste_img, box)
return template
def add_img_to_template(post_folder, template, img_desc):
post_img = Image.open(post_folder + img_desc["file"])
post_img = crop_img_center(post_img, img_desc["crop_width"], img_desc["crop_height"])
post_img = post_img.resize((img_desc["fit_width"], img_desc["fit_height"]), Image.ANTIALIAS)
return paste_img(template, post_img, img_desc["x"], img_desc["y"])
def draw_text_to_img(img, text, font_file, font_size, color, x, y, fit_size):
draw = ImageDraw.Draw(img)
if os.path.exists(font_file):
font = get_max_font_size(font_file, text, fit_size)
draw.rectangle(((x,y), (x + fit_size[0], y + fit_size[1])), fill="yellow")
x, y = get_text_centered_position(font.getsize(text), fit_size, x, y)
draw.text((x, y), text, tuple(color), font=font)
return img
def get_post_text(post_data_dict, text_dict):
if "lang" in text_dict and text_dict["lang"] in post_data_dict:
lang_dict = post_data_dict[text_dict["lang"]]
if "text" in text_dict and text_dict["text"] in lang_dict:
text = lang_dict[text_dict["text"]]
text = fix_arabic_text(text)
return text
return ""
def render_text_to_img(template, text_desc, img_texts, post_folder):
if "post_folder" in text_desc:
pf = text_desc["post_folder"] + "/"
img_texts = load_json(post_folder + pf.replace("//", "/") + "data.json")
if img_texts is None:
return
text = get_post_text(img_texts, text_desc)
font_file = text_desc["font_file"]
font_color = bytes.fromhex(text_desc["font_color"][1:])
font_size = text_desc["font_size"]
x = text_desc["x"]
y = text_desc["y"]
max_width = text_desc["width"]
max_height = text_desc["height"]
return draw_text_to_img(template, text, font_file, font_size, font_color, x, y, (max_width, max_height))
def images_generated_before(desc_file, desc_dict, post_folder):
for ext in desc_dict["save_formats"]:
f = os.path.basename(desc_file)
f, e = os.path.splitext(f)
if os.path.exists(post_folder + f + ext) is False:
return False
return True
def create_post_img(desc_dict, post_folder):
template = open_template(desc_dict["template"])
template_overlay = open_template_overlay(desc_dict["template_overlay"])
if template is None or template_overlay is None:
return False
for img_desc in desc_dict["images"]:
if os.path.exists(post_folder + img_desc["file"]):
add_img_to_template(post_folder, template, img_desc)
else:
return False
template = paste_img(template, template_overlay, 0, 0)
posts = set()
for img_desc in desc_dict["images"]:
path_comps = img_desc["file"].split("/")
if len(path_comps) > 1:
posts.add(path_comps[0])
img_texts = load_json(post_folder + "data.json")
if img_texts is not None or len(posts) > 0:
for text_desc in desc_dict["captions"]:
render_text_to_img(template, text_desc, img_texts, post_folder)
return template
def save_img(img, post_folder, fname, formats):
if os.path.exists(post_folder):
for ext in formats:
if img is not None:
img.save(post_folder + fname + "." + ext)
def image_exists(img_desc, post_folder, ext=".jpg"):
f = os.path.basename(img_desc)
f, e = os.path.splitext(f)
return os.path.exists(post_folder + f + ext)
def copy_img_if_exists(img_desc, post_folder, video_name, fname):
f = os.path.basename(img_desc)
f, e = os.path.splitext(f)
if image_exists(img_desc, post_folder):
copyfile(post_folder + f + ".jpg", post_folder + video_name + "/" + fname + ".jpg")
return True
else:
return False
def copy_descriptor_file(desc_name, dest_dir):
if os.path.exists("descriptors/" + desc_name):
os.makedirs(dest_dir, exist_ok=True)
copyfile("descriptors/" + desc_name, dest_dir + desc_name)
def save_video(imgs_path, video_path, img_duration):
command = "ffmpeg -r " + str(img_duration) + " -i " + imgs_path + "%d" + ".jpg -vcodec mpeg4 -y " + \
video_path + ".mp4"
os.system(command)
def create_post_video(desc_dict, post_folder):
if "videos" in desc_dict:
counter = 0
for video in desc_dict["videos"]:
if not os.path.exists(post_folder + video["name"] + "/"):
os.mkdir(post_folder + video["name"] + "/")
for img_desc in video["images"]:
copy_descriptor_file(img_desc, post_folder)
if not copy_img_if_exists(img_desc, post_folder, video["name"], str(counter)):
img_dict = load_json(post_folder + img_desc)
img = create_post_img(img_dict, post_folder)
if img is False:
continue
f, e = os.path.splitext(img_desc)
save_img(img, post_folder + video["name"] + "/", str(counter), ["jpg"])
counter += 1
save_video(post_folder + video["name"] + "/", post_folder + video["name"] + "/" + video["name"], video["image_duration"])
##MAIN##
if __name__ == "__main__":
'''read the index file from the input directory and generate the corresponding ad images'''
if len(sys.argv) < 2:
print("You must enter the resources directory")
sys.exit(1)
urls_file = sys.argv[1]
out_dirs = download_resources(get_urls(urls_file))
index_file = "index.json"
if len(sys.argv) > 3:
index_file = sys.argv[3]
index_dict = read_index(index_file)
date_folder = datetime.datetime.now().strftime("%Y/%m/%d/")
for post_folder in index_dict:
for desc_file in index_dict[post_folder]:
post_folder_with_date = date_folder + post_folder.replace(".", "")
copy_descriptor_file(desc_file, post_folder_with_date)
desc_dict = read_descriptor(post_folder_with_date + desc_file)
filename, ex = os.path.splitext(desc_file)
if images_generated_before(desc_file, desc_dict, post_folder_with_date) is False:
post_img_combined = create_post_img(desc_dict, post_folder_with_date)
if post_img_combined is not False:
save_img(post_img_combined, post_folder_with_date, filename, desc_dict["save_formats"])
create_post_video(desc_dict, post_folder_with_date)