#!/usr/bin/env python3 """Erzeugt die App-Icons aus einer Silhouetten-Vorlage. Aufruf: python3 tools/make_app_icons.py pfad/zur/silhouette.png Die Vorlage muss eine Silhouette mit Alphakanal sein (Form = Deckkraft). Erzeugt: rinkplanner/assets/icons/app_icon.png 1024x1024 (Desktop/macOS) rinkplanner/assets/icons/android/ic_launcher_192.png Legacy-Launcher .../ic_launcher_foreground_432.png Adaptive Foreground .../ic_launcher_background_432.png Adaptive Background .../ic_launcher_monochrome_432.png Themed Icon """ from pathlib import Path import sys from PIL import Image, ImageDraw REPO = Path(__file__).resolve().parent.parent ICONS = REPO / "rinkplanner" / "assets" / "icons" ANDROID = ICONS / "android" BACKGROUND = (11, 18, 32, 255) # #0b1220, passt zur App-Oberfläche SILHOUETTE = (255, 255, 255, 255) MASTER_SIZE = 1024 CORNER_RADIUS = 220 def load_mask(path: Path) -> Image.Image: source = Image.open(path).convert("RGBA") return source.getchannel("A") def fitted(mask: Image.Image, target_height: int) -> Image.Image: bbox = mask.getbbox() if bbox is None: raise SystemExit("Die Vorlage enthält keine sichtbare Silhouette.") cropped = mask.crop(bbox) scale = target_height / cropped.height size = (max(1, round(cropped.width * scale)), target_height) return cropped.resize(size, Image.LANCZOS) def tinted(mask: Image.Image, color) -> Image.Image: layer = Image.new("RGBA", mask.size, color) layer.putalpha(mask) return layer def rounded_background(size: int, radius: int) -> Image.Image: tile = Image.new("RGBA", (size, size), (0, 0, 0, 0)) draw = ImageDraw.Draw(tile) draw.rounded_rectangle((0, 0, size - 1, size - 1), radius=radius, fill=BACKGROUND) return tile def compose_master(mask: Image.Image) -> Image.Image: tile = rounded_background(MASTER_SIZE, CORNER_RADIUS) shape = fitted(mask, int(MASTER_SIZE * 0.74)) position = ( (MASTER_SIZE - shape.width) // 2, (MASTER_SIZE - shape.height) // 2, ) tile.alpha_composite(tinted(shape, SILHOUETTE), position) return tile def compose_foreground(mask: Image.Image) -> Image.Image: canvas = Image.new("RGBA", (432, 432), (0, 0, 0, 0)) shape = fitted(mask, int(432 * 0.60)) position = ((432 - shape.width) // 2, (432 - shape.height) // 2) canvas.alpha_composite(tinted(shape, SILHOUETTE), position) return canvas def main() -> None: if len(sys.argv) != 2: raise SystemExit("Aufruf: python3 tools/make_app_icons.py ") source = Path(sys.argv[1]) if not source.exists(): raise SystemExit(f"Vorlage nicht gefunden: {source}") ANDROID.mkdir(parents=True, exist_ok=True) mask = load_mask(source) master = compose_master(mask) master.save(ICONS / "app_icon.png") master.resize((192, 192), Image.LANCZOS).save(ANDROID / "ic_launcher_192.png") foreground = compose_foreground(mask) foreground.save(ANDROID / "ic_launcher_foreground_432.png") background = Image.new("RGBA", (432, 432), BACKGROUND) background.save(ANDROID / "ic_launcher_background_432.png") monochrome = Image.new("RGBA", (432, 432), (0, 0, 0, 0)) monochrome.alpha_composite(foreground) monochrome.save(ANDROID / "ic_launcher_monochrome_432.png") print(f"Vorlage: {source}") for path in sorted(ICONS.rglob("*.png")): print(f" {path.relative_to(REPO)}") if __name__ == "__main__": main()