-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawResults.py
398 lines (306 loc) · 14.6 KB
/
drawResults.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
from PIL import Image, ImageDraw, ImageFont
import requests
from io import BytesIO
import datetime
import os
def drawResults(event, standings, account):
print("Draw results")
img = Image.new('RGBA', (1280, 720), color=(255, 255, 255, 255))
results_template = Image.open(
'./results_template.png', 'r').convert("RGBA")
player_bg = Image.open('./results_player.png', 'r').convert("RGBA")
bannerUrl = None
banner = None
if event.get("images", None) is not None and len(event.get("images")) > 0:
bannerUrl = next((i["url"] for i in event["images"]
if i["type"] == "banner"), None)
if bannerUrl == None:
event["images"][-1]["url"]
if bannerUrl != None:
response = requests.get(bannerUrl)
banner = Image.open(BytesIO(response.content)).convert("RGBA")
else:
banner = Image.open("banner/"+account["game"]+".png").convert("RGBA")
banner_w, banner_h = banner.size
goal_w = 1280
goal_h = 360
goal_proportion = goal_w/goal_h
banner_proportion = banner_w/banner_h
if banner_proportion < goal_proportion:
banner = banner.resize(
(goal_w, int(goal_w/banner_w*banner_h)), Image.LANCZOS)
banner_w, banner_h = banner.size
banner = banner.crop(
(0, (banner_h-goal_h)/2, goal_w, goal_h+(banner_h-goal_h)/2))
else:
banner = banner.resize(
(int(goal_h/banner_h*banner_w), goal_h), Image.LANCZOS)
banner_w, banner_h = banner.size
img.paste(banner, (int(goal_w/2-banner_w/2), 48))
img.alpha_composite(results_template)
fnt_top = ImageFont.truetype('./smash_font.ttf', 42)
d = ImageDraw.Draw(img, "RGBA")
title_text_top = event["tournament"]
if event["tournament_multievent"]:
title_text_top += " - "+event["name"]
if len(title_text_top) > 80:
title_text_top = title_text_top[:80]+"…"
_, _, w, h = d.textbbox((0, 0), title_text_top, font=fnt_top)
d.text((640-w/2, 0), title_text_top, font=fnt_top,
fill=(255, 255, 255), align="center")
if standings.get("multiphase"):
phase_text = standings.get("phase").get("name")
_, _, w, h = d.textbbox((0, 0), phase_text, font=fnt_top)
d.rectangle(((640-w/2-16, 48), (640+w/2+16, 48+h+6)),
fill=(45, 45, 45))
d.text((640-w/2, 48), phase_text, font=fnt_top,
fill=(255, 255, 255), align="center")
fnt = ImageFont.truetype('./smash_font.ttf', 32)
# Location if applies
location = ""
if event.get("tournament_venueName") or event.get("tournament_venueAddress"):
if event.get("tournament_venueName"):
location += event.get("tournament_venueName")+", "
if event.get("tournament_venueAddress"):
splitted = event.get("tournament_venueAddress").split(",")
if len(splitted) == 1:
location += splitted[0]
if len(splitted) == 2:
location += splitted[0]
if len(splitted) == 3:
location += splitted[1]
if len(splitted) == 4:
location += splitted[-3]+", "+splitted[-2]
if len(splitted) == 5:
location += splitted[-3]
title_text_bottom = ("Online" if event.get(
"isOnline") else location) + " - "
title_text_bottom += str(standings.get("standings").get("pageInfo").get(
"total")) + " " + account["text-results-participants"]
data_time = datetime.datetime.fromtimestamp(event["startAt"])
data = data_time.strftime(account["text-results-timeformat"])
title_text_bottom += " - " + data
_, _, w, h = d.textbbox((0, 0), title_text_bottom, font=fnt)
d.text((640-w/2, 408), title_text_bottom, font=fnt,
fill=(255, 255, 255), align="center")
pos_y = 455
pos_x = 40
fnt_results = ImageFont.truetype('./smash_font.ttf', 32)
fnt_results_doubles_top = ImageFont.truetype('./smash_font.ttf', 20)
fnt_results_doubles_bottom = ImageFont.truetype('./smash_font.ttf', 16)
for i, entrant in enumerate(standings["standings"]["nodes"][0:8]):
img.paste(player_bg, (pos_x, pos_y))
d.text((pos_x+18, pos_y+6), str(entrant["placement"]) + ". ",
font=fnt_results, fill=(43, 43, 43), align="left")
entry_text = entrant["entrant"]["name"]
teamMembers = []
if len(entrant["entrant"].get("participants", [])) > 1:
for p in entrant["entrant"].get("participants", []):
teamMembers.append(p.get("player", {}).get("gamerTag", ""))
teamMemberNames = " / ".join(teamMembers)
equalSets = set(entry_text.split(" / ")) == set(teamMembers)
if len(entrant["entrant"].get("participants", [])) <= 1 or equalSets:
# Singles
# Cut out giantic names
_, _, w, h = d.textbbox((0, 0), entry_text, font=fnt_results)
if w > 400:
while w > 400:
entry_text = entry_text[0:-1]
_, _, w, h = d.textbbox(
(0, 0), entry_text+"…", font=fnt_results)
entry_text += "…"
d.text((pos_x+52, pos_y+6), entry_text,
font=fnt_results, fill=(43, 43, 43), align="left")
else:
# Doubles
# Cut out giantic names
_, _, w, h = d.textbbox(
(0, 0), entry_text, font=fnt_results_doubles_top)
if w > 400:
while w > 400:
entry_text = entry_text[0:-1]
_, _, w, h = d.textbbox((0, 0),
entry_text+"…", font=fnt_results_doubles_top)
entry_text += "…"
d.text((pos_x+52, pos_y+6), entry_text,
font=fnt_results_doubles_top, fill=(43, 43, 43), align="left")
# Cut out giantic names
_, _, w, h = d.textbbox(
(0, 0), teamMemberNames, font=fnt_results_doubles_bottom)
if w > 400:
while w > 400:
teamMemberNames = teamMemberNames[0:-1]
_, _, w, h = d.textbbox((0, 0),
teamMemberNames+"…", font=fnt_results_doubles_bottom)
teamMemberNames += "…"
d.text((pos_x+52, pos_y+6+20), teamMemberNames,
font=fnt_results_doubles_bottom, fill=(43, 43, 43), align="left")
load_local_portraits = False
if os.path.exists("./portraits/"+account["game"]):
load_local_portraits = True
if entrant.get("char_usage"):
chars_width = 42 * min(len(entrant.get("char_usage")), 4) - 42
for j, char in enumerate(entrant.get("char_usage").items()):
icon = None
try:
if not load_local_portraits:
response = requests.get(char[1]["icon"])
icon = Image.open(
BytesIO(response.content)).convert("RGBA")
else:
icon = Image.open(
"./portraits/"+account["game"]+"/"+char[1]["name"]+".png").convert("RGBA")
icon = icon.resize((42, 42), Image.LANCZOS)
img.alpha_composite(
icon, (pos_x+540-chars_width+42*j, pos_y+4))
except Exception as e:
print("No character icon?")
print(e)
if j == 2 and len(entrant.get("char_usage")) > 4:
d.text((pos_x+540, pos_y+6), "+"+str(len(entrant.get("char_usage"))-3),
font=fnt_results, fill=(43, 43, 43), align="left")
break
if entrant.get("dq"):
d.text((pos_x+540, pos_y+6), "DQ", font=fnt_results,
fill=(43, 43, 43), align="left")
pos_y += 56
if (i == 3):
pos_y = 455
pos_x = 655
fnt_footer = ImageFont.truetype('./smash_font.ttf', 24)
d.text((20, 686), account["text-results-generated-by-before"]+" @"+account["handle"]+" " +
account["text-results-generated-by-after"], font=fnt_footer, fill=(255, 255, 255), align="center")
img.save('media.png')
def drawResults8x9(event, standings, account, page=1):
print("Draw results 8x9")
img = Image.new('RGBA', (640, 720), color=(255, 255, 255, 255))
results_template = Image.open(
'./results_template8x9.png', 'r').convert("RGBA")
player_bg = Image.open('./results_player.png', 'r').convert("RGBA")
player_bg = player_bg.crop((0, 0, 590, 48))
bannerUrl = None
banner = None
if event.get("images", None) is not None and len(event.get("images")) > 0:
bannerUrl = next((i["url"] for i in event["images"]
if i["type"] == "banner"), None)
if bannerUrl == None:
event["images"][-1]["url"]
if bannerUrl != None:
response = requests.get(bannerUrl)
banner = Image.open(BytesIO(response.content)).convert("RGBA")
else:
banner = Image.open("banner/"+account["game"]+".png").convert("RGBA")
banner_w, banner_h = banner.size
goal_w = 640
goal_h = 180
goal_proportion = goal_w/goal_h
banner_proportion = banner_w/banner_h
if banner_proportion < goal_proportion:
banner = banner.resize(
(goal_w, int(goal_w/banner_w*banner_h)), Image.LANCZOS)
banner_w, banner_h = banner.size
banner = banner.crop(
(0, (banner_h-goal_h)/2, goal_w, goal_h+(banner_h-goal_h)/2))
else:
banner = banner.resize(
(int(goal_h/banner_h*banner_w), goal_h), Image.LANCZOS)
banner_w, banner_h = banner.size
img.paste(banner, (int(goal_w/2-banner_w/2), 48))
img.alpha_composite(results_template)
fnt_top = ImageFont.truetype('./smash_font.ttf', 24)
d = ImageDraw.Draw(img, "RGBA")
title_text_top = event["tournament"]
if event["tournament_multievent"]:
title_text_top += " - "+event["name"]
_, _, w, h = d.textbbox((0, 0), title_text_top, font=fnt_top)
d.text((320-w/2, 8), title_text_top, font=fnt_top,
fill=(255, 255, 255), align="center")
if standings.get("multiphase"):
phase_text = standings.get("phase").get("name")
_, _, w, h = d.textbbox((0, 0), phase_text, font=fnt_top)
d.rectangle(((320-w/2-16, 48), (320+w/2+16, 48+h+6)),
fill=(43, 43, 43))
d.text((320-w/2, 48), phase_text, font=fnt_top,
fill=(255, 255, 255), align="center")
fnt = ImageFont.truetype('./smash_font.ttf', 22)
# Location if applies
location = ""
if event.get("tournament_venueName") or event.get("tournament_venueAddress"):
if event.get("tournament_venueName"):
location += event.get("tournament_venueName")+", "
if event.get("tournament_venueAddress"):
splitted = event.get("tournament_venueAddress").split(",")
if len(splitted) == 1:
location += splitted[0]
if len(splitted) == 2:
location += splitted[0]
if len(splitted) == 3:
location += splitted[1]
if len(splitted) == 4:
location += splitted[-3]+", "+splitted[-2]
if len(splitted) == 5:
location += splitted[-3]
title_text_bottom = ("Online" if event.get(
"isOnline") else location) + " - "
title_text_bottom += str(standings.get("standings").get("pageInfo").get(
"total")) + " " + account["text-results-participants"]
data_time = datetime.datetime.fromtimestamp(event["startAt"])
data = data_time.strftime(account["text-results-timeformat"])
title_text_bottom += " - " + data
_, _, w, h = d.textbbox((0, 0), title_text_bottom, font=fnt)
d.text((320-w/2, 234), title_text_bottom, font=fnt,
fill=(255, 255, 255), align="center")
pos_y = 270
pos_x = 25
fnt_results = ImageFont.truetype('./smash_font.ttf', 24)
for i, entrant in enumerate(standings["standings"]["nodes"][((8*(page-1))+0):((8*(page-1))+8)]):
img.paste(player_bg, (pos_x, pos_y))
entry_text = str(entrant["placement"]) + \
". " + entrant["entrant"]["name"]
_, _, w, h = d.textbbox((0, 0), entry_text, font=fnt_results)
# Cut out giantic names
if w > 400:
while w > 400:
entry_text = entry_text[0:-1]
_, _, w, h = d.textbbox(
(0, 0), entry_text+"…", font=fnt_results)
entry_text += "…"
d.text((pos_x+18, pos_y+8), entry_text,
font=fnt_results, fill=(43, 43, 43), align="left")
load_local_portraits = False
if os.path.exists("./portraits/"+account["game"]):
load_local_portraits = True
if entrant.get("char_usage"):
chars_width = 42 * min(len(entrant.get("char_usage")), 4) - 42
for j, char in enumerate(entrant.get("char_usage").items()):
icon = None
try:
if not load_local_portraits:
response = requests.get(char[1]["icon"])
icon = Image.open(
BytesIO(response.content)).convert("RGBA")
else:
icon = Image.open(
"./portraits/"+account["game"]+"/"+char[1]["name"]+".png").convert("RGBA")
icon = icon.resize((42, 42), Image.LANCZOS)
img.alpha_composite(
icon, (pos_x+540-chars_width+42*j, pos_y+4))
except Exception as e:
print("No character icon?")
print(e)
if j == 2 and len(entrant.get("char_usage")) > 4:
d.text((pos_x+540+6, pos_y+12), "+"+str(len(entrant.get("char_usage"))-3),
font=fnt_results, fill=(43, 43, 43), align="left")
break
if entrant.get("dq"):
d.text((pos_x+540+6, pos_y+12), "DQ", font=fnt_results,
fill=(43, 43, 43), align="left")
pos_y += 52
fnt_footer = ImageFont.truetype('./smash_font.ttf', 20)
d.text((10, 691), account["text-results-generated-by-before"]+" @"+account["handle"]+" " +
account["text-results-generated-by-after"], font=fnt_footer, fill=(255, 255, 255), align="center")
d.text((640-40, 690), str(page)+"/2", font=fnt,
fill=(255, 255, 255), align="center")
img.save('media.png' if page == 1 else 'media2.png')
if page == 1:
drawResults8x9(event, standings, account, page=2)