10
10
import platform
11
11
import shlex
12
12
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 )
13
36
14
37
15
38
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):
26
49
27
50
if existing_etag != etag :
28
51
print (f'Downloading and converting { image_type } for entity with ID { entity_id } ...' )
52
+ os .makedirs (os .path .dirname (temp_file ), exist_ok = True )
29
53
with open (temp_file , 'wb' ) as f :
30
54
f .write (requests .get (f'{ api_url } /{ href } ' , auth = (user , passwd )).content )
31
55
@@ -39,6 +63,11 @@ def download_image(image_type: str, entity_id: str, file: dict):
39
63
host = platform .node ()
40
64
host_bg_color = 'black'
41
65
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
+
42
71
config_file = 'domlogo-files/config.yaml'
43
72
if os .path .isfile (config_file ) and os .access (config_file , os .R_OK ):
44
73
with open (config_file , 'r' ) as f :
@@ -172,7 +201,7 @@ def download_image(image_type: str, entity_id: str, file: dict):
172
201
last_seen = (submission_id , judging_id , team_id )
173
202
new_filename = f'domlogo-files/photos/{ team_id } .png'
174
203
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 )
176
205
team_image .update (filename = new_filename )
177
206
metadata_text .update (f's{ submission_id } / { submission_data ["problem_id" ]} / { submission_data ["language_id" ]} ' )
178
207
results_text .update ('Busy compiling.' )
0 commit comments