1010
1111parent_app = None
1212
13- canvas_size = (780 , 520 )
13+ static_folder = os .path .join (os .path .dirname (__file__ ), 'static' )
14+
15+ background = Image .open (os .path .join (static_folder , 'background.png' )).convert ('RGBA' )
1416
15- background_color = (255 , 71 , 0 )
16- overlay_color = (55 , 58 , 60 )
1717text_color = (255 , 255 , 255 )
1818
1919overlay_box = (0 , 392 , 780 , 520 )
2020icon_position = (24 , 416 )
2121base_title_position = (36 , 404 )
2222base_author_position = (36 , 460 )
2323base_text_space = 530
24- logo_position = (576 , 418 )
25-
26- def platform_borders (platform ):
27- if platform == 'emery' :
28- return {
29- 'image' : Image .open (os .path .join (parent_app .static_folder , 'emery-border.png' )).convert ("RGBA" ),
30- 'fallback' : Image .open (os .path .join (parent_app .static_folder , 'fallback-emery.png' )).convert ("RGBA" ),
31- 'offset' : (65 , 79 )
32- }
33-
34- if platform == 'diorite' or platform == 'flint' :
35- return {
36- 'image' : Image .open (os .path .join (parent_app .static_folder , 'diorite-border.png' )).convert ("RGBA" ),
37- 'fallback' : Image .open (os .path .join (parent_app .static_folder , 'fallback-bw.png' )).convert ("RGBA" ),
38- 'offset' : (54 , 110 )
39- }
40-
41- if platform == 'basalt' :
42- return {
43- 'image' : Image .open (os .path .join (parent_app .static_folder , 'basalt-border.png' )).convert ("RGBA" ),
44- 'fallback' : Image .open (os .path .join (parent_app .static_folder , 'fallback-basalt.png' )).convert ("RGBA" ),
45- 'offset' : (88 ,111 )
46- }
4724
48- if platform == 'chalk' :
49- return {
50- 'image' : Image .open (os .path .join (parent_app .static_folder , 'chalk-border.png' )).convert ("RGBA" ),
51- 'fallback' : Image .open (os .path .join (parent_app .static_folder , 'fallback-chalk.png' )).convert ("RGBA" ),
52- 'offset' : (71 ,105 )
53- }
54-
55- return {
56- 'image' : Image .open (os .path .join (parent_app .static_folder , 'aplite-border.png' )).convert ("RGBA" ),
57- 'fallback' : Image .open (os .path .join (parent_app .static_folder , 'fallback-bw.png' )).convert ("RGBA" ),
25+ icon_mask = Image .open (os .path .join (static_folder , 'icon-mask.png' )).convert ('L' )
26+ chalk_mask = Image .open (os .path .join (static_folder , 'chalk-mask.png' )).convert ('L' )
27+
28+ platform_borders = {
29+ 'aplite' : {
30+ 'image' : Image .open (os .path .join (static_folder , 'aplite-border.png' )).convert ("RGBA" ),
31+ 'fallback' : Image .open (os .path .join (static_folder , 'fallback-bw.png' )).convert ("RGBA" ),
5832 'offset' : (68 ,106 )
33+ },
34+ 'basalt' : {
35+ 'image' : Image .open (os .path .join (static_folder , 'basalt-border.png' )).convert ("RGBA" ),
36+ 'fallback' : Image .open (os .path .join (static_folder , 'fallback-basalt.png' )).convert ("RGBA" ),
37+ 'offset' : (88 ,111 )
38+ },
39+ 'chalk' : {
40+ 'image' : Image .open (os .path .join (static_folder , 'chalk-border.png' )).convert ("RGBA" ),
41+ 'fallback' : Image .open (os .path .join (static_folder , 'fallback-chalk.png' )).convert ("RGBA" ),
42+ 'offset' : (71 ,105 )
43+ },
44+ 'diorite' : {
45+ 'image' : Image .open (os .path .join (static_folder , 'diorite-border.png' )).convert ("RGBA" ),
46+ 'fallback' : Image .open (os .path .join (static_folder , 'fallback-bw.png' )).convert ("RGBA" ),
47+ 'offset' : (54 , 110 )
48+ },
49+ 'emery' : {
50+ 'image' : Image .open (os .path .join (static_folder , 'emery-border.png' )).convert ("RGBA" ),
51+ 'fallback' : Image .open (os .path .join (static_folder , 'fallback-emery.png' )).convert ("RGBA" ),
52+ 'offset' : (65 , 79 )
53+ },
54+ 'flint' : {
55+ 'image' : Image .open (os .path .join (static_folder , 'diorite-border.png' )).convert ("RGBA" ),
56+ 'fallback' : Image .open (os .path .join (static_folder , 'fallback-bw.png' )).convert ("RGBA" ),
57+ 'offset' : (54 , 110 )
5958 }
59+ }
60+
61+ font_large = ImageFont .truetype (os .path .join (static_folder , 'Lato-Bold.ttf' ), 48 )
62+ font_small = ImageFont .truetype (os .path .join (static_folder , 'Lato-Regular.ttf' ), 32 )
6063
6164def preferred_grouping (platforms ):
6265 order = [['diorite' , 'emery' ], ['flint' , 'emery' ], ['basalt' , 'emery' ], ['chalk' , 'emery' ],
@@ -94,11 +97,10 @@ def draw_text_ellipsized(draw, text, font, xy, max_width):
9497 draw .text (xy , trimmed + ellipsis , font = font , fill = text_color )
9598
9699def platform_image_in_border (canvas , image_url , top_left , platform ):
97- border = platform_borders ( platform )
100+ border = platform_borders [ platform ]
98101 img = load_image_from_url (image_url , border ['fallback' ])
99102
100103 if platform == 'chalk' :
101- chalk_mask = Image .open (os .path .join (parent_app .static_folder , 'chalk-mask.png' )).convert ('L' )
102104 img .putalpha (chalk_mask )
103105
104106 ix = top_left [0 ] + border ['offset' ][0 ]
@@ -109,16 +111,11 @@ def platform_image_in_border(canvas, image_url, top_left, platform):
109111 canvas .alpha_composite (border ['image' ], top_left )
110112
111113def generate_preview_image (title , developer , icon , screenshots ):
112- canvas = Image . new ( "RGBA" , canvas_size , background_color )
114+ canvas = background . copy ( )
113115 draw = ImageDraw .Draw (canvas )
114116
115- logo = Image .open (os .path .join (parent_app .static_folder , 'rebble-appstore-logo.png' )).convert ('RGBA' )
116-
117- draw .rectangle (overlay_box , fill = overlay_color )
118- canvas .alpha_composite (logo , logo_position )
119-
120117 platforms = preferred_grouping (screenshots .keys ())
121- start_x = ceil ((canvas .width - sum (platform_borders ( platform ) ['image' ].width for platform in platforms )) / 2 )
118+ start_x = ceil ((canvas .width - sum (platform_borders [ platform ] ['image' ].width for platform in platforms )) / 2 )
122119
123120 for platform in platforms :
124121 platform_image_in_border (
@@ -127,7 +124,7 @@ def generate_preview_image(title, developer, icon, screenshots):
127124 top_left = (start_x , 0 ),
128125 platform = platform
129126 )
130- start_x += platform_borders ( platform ) ['image' ].width
127+ start_x += platform_borders [ platform ] ['image' ].width
131128
132129 title_position = base_title_position
133130 author_position = base_author_position
@@ -140,17 +137,12 @@ def generate_preview_image(title, developer, icon, screenshots):
140137 icon_image = None
141138
142139 if icon_image :
143- icon_mask = Image .open (os .path .join (parent_app .static_folder , 'icon-mask.png' )).convert ('L' )
144-
145140 icon_image .putalpha (ImageChops .multiply (icon_mask , icon_image .split ()[3 ]))
146141 canvas .alpha_composite (icon_image , icon_position )
147142 title_position = (title_position [0 ] + 88 , title_position [1 ])
148143 author_position = (author_position [0 ] + 88 , author_position [1 ])
149144 text_space -= 88
150145
151- font_large = ImageFont .truetype (os .path .join (parent_app .static_folder , 'Lato-Bold.ttf' ), 48 )
152- font_small = ImageFont .truetype (os .path .join (parent_app .static_folder , 'Lato-Regular.ttf' ), 32 )
153-
154146 draw_text_ellipsized (draw , title , font_large , title_position , text_space )
155147 draw_text_ellipsized (draw , developer , font_small , author_position , text_space )
156148
0 commit comments