- make_app_icons.py um --inverted sowie --background/--figure erweitert - Master, Legacy- und Adaptive-Icons neu erzeugt; Monochrom bleibt alphabasiert für Androids Themed Icons
134 lines
4.7 KiB
Python
134 lines
4.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Erzeugt die App-Icons aus einer Silhouetten-Vorlage.
|
|
|
|
Aufruf:
|
|
python3 tools/make_app_icons.py pfad/zur/silhouette.png
|
|
python3 tools/make_app_icons.py silhouette.png --inverted
|
|
python3 tools/make_app_icons.py silhouette.png --background "#ffffff" --figure "#000000"
|
|
|
|
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 argparse
|
|
|
|
from PIL import Image, ImageDraw
|
|
|
|
REPO = Path(__file__).resolve().parent.parent
|
|
ICONS = REPO / "rinkplanner" / "assets" / "icons"
|
|
ANDROID = ICONS / "android"
|
|
|
|
DEFAULT_BACKGROUND = "#0b1220" # passt zur App-Oberfläche
|
|
DEFAULT_FIGURE = "#ffffff"
|
|
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 hex_color(value: str) -> tuple:
|
|
text = value.strip().lstrip("#")
|
|
if len(text) == 6:
|
|
text += "ff"
|
|
if len(text) != 8:
|
|
raise SystemExit(f"Ungültige Farbe: {value}")
|
|
return tuple(int(text[i:i + 2], 16) for i in (0, 2, 4, 6))
|
|
|
|
|
|
def rounded_background(size: int, radius: int, color: tuple) -> 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=color)
|
|
return tile
|
|
|
|
|
|
def compose_master(mask: Image.Image, background: tuple, figure: tuple) -> Image.Image:
|
|
tile = rounded_background(MASTER_SIZE, CORNER_RADIUS, background)
|
|
shape = fitted(mask, int(MASTER_SIZE * 0.74))
|
|
position = (
|
|
(MASTER_SIZE - shape.width) // 2,
|
|
(MASTER_SIZE - shape.height) // 2,
|
|
)
|
|
tile.alpha_composite(tinted(shape, figure), position)
|
|
return tile
|
|
|
|
|
|
def compose_foreground(mask: Image.Image, figure: tuple) -> 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, figure), position)
|
|
return canvas
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser(description="App-Icons aus einer Silhouette erzeugen.")
|
|
parser.add_argument("source", help="PNG mit Alphakanal-Silhouette")
|
|
parser.add_argument("--background", default=DEFAULT_BACKGROUND)
|
|
parser.add_argument("--figure", default=DEFAULT_FIGURE)
|
|
parser.add_argument(
|
|
"--inverted",
|
|
action="store_true",
|
|
help="weißer Hintergrund, schwarze Figur",
|
|
)
|
|
args = parser.parse_args()
|
|
|
|
background = hex_color("#ffffff" if args.inverted else args.background)
|
|
figure = hex_color("#000000" if args.inverted else args.figure)
|
|
# Themed (monochrome) icons are tinted by the system; alpha is what counts.
|
|
monochrome = hex_color("#ffffff")
|
|
|
|
source = Path(args.source)
|
|
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, background, figure)
|
|
master.save(ICONS / "app_icon.png")
|
|
master.resize((192, 192), Image.LANCZOS).save(ANDROID / "ic_launcher_192.png")
|
|
|
|
foreground = compose_foreground(mask, figure)
|
|
foreground.save(ANDROID / "ic_launcher_foreground_432.png")
|
|
|
|
background_layer = Image.new("RGBA", (432, 432), background)
|
|
background_layer.save(ANDROID / "ic_launcher_background_432.png")
|
|
|
|
monochrome_layer = Image.new("RGBA", (432, 432), (0, 0, 0, 0))
|
|
monochrome_layer.alpha_composite(compose_foreground(mask, monochrome))
|
|
monochrome_layer.save(ANDROID / "ic_launcher_monochrome_432.png")
|
|
|
|
print(f"Vorlage: {source}")
|
|
print(f"Hintergrund: {args.background} Figur: {args.figure}")
|
|
for path in sorted(ICONS.rglob("*.png")):
|
|
print(f" {path.relative_to(REPO)}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|