|
| 1 | +import os |
| 2 | +import io |
| 3 | +import requests |
| 4 | + |
| 5 | +from flask import Flask |
| 6 | +from PIL import Image, ImageDraw, ImageFont, ImageChops |
| 7 | +from math import ceil |
| 8 | + |
| 9 | +from .utils import valid_platforms |
| 10 | + |
| 11 | +parent_app = None |
| 12 | + |
| 13 | +canvas_size = (780, 520) |
| 14 | + |
| 15 | +background_color=(255, 71, 0) |
| 16 | +overlay_color=(55, 58, 60) |
| 17 | +text_color=(255, 255, 255) |
| 18 | + |
| 19 | +overlay_box=(0, 392, 780, 520) |
| 20 | +icon_position=(24, 416) |
| 21 | +base_title_position=(36, 404) |
| 22 | +base_author_position=(36, 460) |
| 23 | +base_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 | + } |
| 47 | + |
| 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"), |
| 58 | + 'offset': (68,106) |
| 59 | + } |
| 60 | + |
| 61 | +def preferred_grouping(platforms): |
| 62 | + order = [['diorite', 'emery'], ['flint', 'emery'], ['basalt', 'emery'], ['chalk', 'emery'], |
| 63 | + ['basalt', 'diorite'], ['basalt', 'flint'], ['basalt', 'chalk'], ['basalt', 'aplite'], |
| 64 | + ['flint'], ['emery'], ['diorite'], ['chalk'], ['basalt'], ['aplite']] |
| 65 | + for selection in order: |
| 66 | + if len(selection) == len(selection & platforms): |
| 67 | + return selection |
| 68 | + |
| 69 | +def load_image_from_url(url, fallback): |
| 70 | + try: |
| 71 | + resp = requests.get(url, timeout=5) |
| 72 | + resp.raise_for_status() |
| 73 | + return Image.open(io.BytesIO(resp.content)).convert("RGBA") |
| 74 | + except Exception as e: |
| 75 | + if fallback: |
| 76 | + return fallback |
| 77 | + raise e |
| 78 | + |
| 79 | +def draw_text_ellipsized(draw, text, font, xy, max_width): |
| 80 | + if draw.textlength(text, font=font) <= max_width: |
| 81 | + draw.text(xy, text, font=font, fill=text_color) |
| 82 | + return |
| 83 | + |
| 84 | + ellipsis = "…" |
| 85 | + ellipsis_width = draw.textlength(ellipsis, font=font) |
| 86 | + |
| 87 | + trimmed = "" |
| 88 | + for char in text: |
| 89 | + if draw.textlength(trimmed + char, font=font) + ellipsis_width <= max_width: |
| 90 | + trimmed += char |
| 91 | + else: |
| 92 | + break |
| 93 | + |
| 94 | + draw.text(xy, trimmed + ellipsis, font=font, fill=text_color) |
| 95 | + |
| 96 | +def platform_image_in_border(canvas, image_url, top_left, platform): |
| 97 | + border = platform_borders(platform) |
| 98 | + img = load_image_from_url(image_url, border['fallback']) |
| 99 | + |
| 100 | + if platform == 'chalk': |
| 101 | + chalk_mask = Image.open(os.path.join(parent_app.static_folder, 'chalk-mask.png')).convert('L') |
| 102 | + img.putalpha(chalk_mask) |
| 103 | + |
| 104 | + ix = top_left[0] + border['offset'][0] |
| 105 | + iy = top_left[1] + border['offset'][1] |
| 106 | + |
| 107 | + canvas.alpha_composite(img, (ix, iy)) |
| 108 | + |
| 109 | + canvas.alpha_composite(border['image'], top_left) |
| 110 | + |
| 111 | +def generate_preview_image(title, developer, icon, screenshots): |
| 112 | + canvas = Image.new("RGBA", canvas_size, background_color) |
| 113 | + draw = ImageDraw.Draw(canvas) |
| 114 | + |
| 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 | + |
| 120 | + platforms = preferred_grouping(screenshots.keys()) |
| 121 | + start_x = ceil((canvas.width - sum(platform_borders(platform)['image'].width for platform in platforms)) / 2) |
| 122 | + |
| 123 | + for platform in platforms: |
| 124 | + platform_image_in_border( |
| 125 | + canvas=canvas, |
| 126 | + image_url=screenshots[platform], |
| 127 | + top_left=(start_x, 0), |
| 128 | + platform=platform |
| 129 | + ) |
| 130 | + start_x += platform_borders(platform)['image'].width |
| 131 | + |
| 132 | + title_position = base_title_position |
| 133 | + author_position = base_author_position |
| 134 | + text_space = base_text_space |
| 135 | + |
| 136 | + icon_image = None |
| 137 | + try: |
| 138 | + icon_image = load_image_from_url(icon, None) |
| 139 | + except Exception as e: |
| 140 | + icon_image = None |
| 141 | + |
| 142 | + if icon_image: |
| 143 | + icon_mask = Image.open(os.path.join(parent_app.static_folder, 'icon-mask.png')).convert('L') |
| 144 | + |
| 145 | + icon_image.putalpha(ImageChops.multiply(icon_mask, icon_image.split()[3])) |
| 146 | + canvas.alpha_composite(icon_image, icon_position) |
| 147 | + title_position = (title_position[0] + 88, title_position[1]) |
| 148 | + author_position = (author_position[0] + 88, author_position[1]) |
| 149 | + text_space -= 88 |
| 150 | + |
| 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 | + |
| 154 | + draw_text_ellipsized(draw, title, font_large, title_position, text_space) |
| 155 | + draw_text_ellipsized(draw, developer, font_small, author_position, text_space) |
| 156 | + |
| 157 | + buffer = io.BytesIO() |
| 158 | + canvas.save(buffer, format='PNG') |
| 159 | + buffer.seek(0) |
| 160 | + return buffer.getvalue() |
| 161 | + |
| 162 | +def init_app(app): |
| 163 | + global parent_app |
| 164 | + parent_app = app |
0 commit comments