Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d651f39

Browse files
committedApr 10, 2024·
Make domlogo more robust when images are missing or folders not created
yet.
1 parent 0e59a47 commit d651f39

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed
 

‎domlogo/domlogo.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,29 @@
1010
import platform
1111
import shlex
1212
import yaml
13+
from PIL import Image, ImageDraw, ImageFont
14+
15+
16+
def generate_placeholder(text: str, path: str, background_color, width: int, height: int):
17+
image = Image.new("RGB", (width, height), background_color)
18+
draw = ImageDraw.Draw(image)
19+
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 72)
20+
text_bbox = draw.textbbox((0, 0), text, font=font)
21+
text_width = text_bbox[2] - text_bbox[0]
22+
text_height = text_bbox[3] - text_bbox[1]
23+
text_x = (width - text_width) / 2
24+
text_y = (height - text_height) / 2
25+
draw.text((text_x, text_y), text, fill=(255,255,255), font=font)
26+
27+
# Add some hint how to replace the image.
28+
small_font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 16)
29+
placeholder_text = f'(Replace this placeholder image at {path})'
30+
text_bbox = draw.textbbox((0, 0), placeholder_text, font=small_font)
31+
text_width = text_bbox[2] - text_bbox[0]
32+
text_x = (width - text_width) / 2
33+
text_y = text_y + 1.4*text_height
34+
draw.text((text_x, text_y), placeholder_text, fill=(195,196,199), font=small_font)
35+
image.save(path)
1336

1437

1538
def download_image(image_type: str, entity_id: str, file: dict):
@@ -26,6 +49,7 @@ def download_image(image_type: str, entity_id: str, file: dict):
2649

2750
if existing_etag != etag:
2851
print(f'Downloading and converting {image_type} for entity with ID {entity_id}...')
52+
os.makedirs(os.path.dirname(temp_file), exist_ok=True)
2953
with open(temp_file, 'wb') as f:
3054
f.write(requests.get(f'{api_url}/{href}', auth=(user, passwd)).content)
3155

@@ -39,6 +63,11 @@ def download_image(image_type: str, entity_id: str, file: dict):
3963
host = platform.node()
4064
host_bg_color = 'black'
4165

66+
idle_image = 'domlogo-files/photos/idle.png'
67+
if not os.path.isfile(idle_image):
68+
os.makedirs(os.path.dirname(idle_image), exist_ok=True)
69+
generate_placeholder('judgehost idle', idle_image, (10,75,120), 1024, 768)
70+
4271
config_file = 'domlogo-files/config.yaml'
4372
if os.path.isfile(config_file) and os.access(config_file, os.R_OK):
4473
with open(config_file, 'r') as f:
@@ -172,7 +201,7 @@ def download_image(image_type: str, entity_id: str, file: dict):
172201
last_seen = (submission_id, judging_id, team_id)
173202
new_filename = f'domlogo-files/photos/{team_id}.png'
174203
if not os.path.isfile(new_filename):
175-
new_filename = f'domlogo-files/photos/crew.png'
204+
generate_placeholder(f'team {team_id}', new_filename, (137,28,28), 1024, 768)
176205
team_image.update(filename=new_filename)
177206
metadata_text.update(f's{submission_id} / {submission_data["problem_id"]} / {submission_data["language_id"]}')
178207
results_text.update('Busy compiling.')

0 commit comments

Comments
 (0)
Please sign in to comment.