80 lines
3.9 KiB
Python
80 lines
3.9 KiB
Python
"""Export Composer artwork with Xcode, then resize with Pillow.
|
|
|
|
Run on macOS with Xcode and Pillow installed: python3 scripts/generate-icons.py
|
|
"""
|
|
import json
|
|
from pathlib import Path
|
|
import shutil
|
|
import subprocess
|
|
import tempfile
|
|
from PIL import Image
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
ASSETS = ROOT / "apps/mobile/assets"
|
|
ICTOOL = "/Applications/Xcode.app/Contents/Applications/Icon Composer.app/Contents/Executables/ictool"
|
|
|
|
|
|
def export(source, output, rendition="Default"):
|
|
subprocess.run([
|
|
ICTOOL, str(source), "--export-image", "--output-file", str(output),
|
|
"--platform", "iOS", "--rendition", rendition,
|
|
"--width", "1024", "--height", "1024", "--scale", "1",
|
|
], check=True)
|
|
return Image.open(output).convert("RGBA")
|
|
|
|
|
|
def save(image, path, size=1024):
|
|
image.resize((size, size), Image.Resampling.LANCZOS).save(ROOT / path)
|
|
|
|
|
|
with tempfile.TemporaryDirectory(prefix="otunes-icons-") as directory:
|
|
temp = Path(directory)
|
|
for variant, composer in [("production", "oTunes"), ("development", "oTunes Dev"), ("preview", "oTunes Preview")]:
|
|
source = ASSETS / f"{composer}.icon"
|
|
icon = export(source, temp / "icon.png")
|
|
dark = export(source, temp / "dark.png", "Dark")
|
|
suffix = "" if variant == "production" else f"-{variant}"
|
|
save(icon, f"apps/mobile/assets/icon{suffix}.png")
|
|
|
|
# Android uses the Composer artwork without Apple's rounded glass base.
|
|
document = json.loads((source / "icon.json").read_text())
|
|
layer = document["groups"][0]["layers"][0]
|
|
artwork = Image.open(source / "Assets" / layer["image-name"]).convert("RGBA")
|
|
position = layer["position"]
|
|
scale = position["scale"]
|
|
artwork = artwork.resize(tuple(round(size * scale) for size in artwork.size), Image.Resampling.LANCZOS)
|
|
x, y = position["translation-in-points"]
|
|
foreground = Image.new("RGBA", (1024, 1024))
|
|
foreground.alpha_composite(artwork, (round((1024 - artwork.width) / 2 + x), round((1024 - artwork.height) / 2 + y)))
|
|
# Android's adaptive canvas includes an outer area reserved for motion.
|
|
adaptive = Image.new("RGBA", (1024, 1024))
|
|
adaptive.alpha_composite(foreground.resize((682, 682), Image.Resampling.LANCZOS), (171, 171))
|
|
save(adaptive, f"apps/mobile/assets/android-icon-foreground{suffix}.png")
|
|
# Use the rendered background's center column to retain Composer colors.
|
|
background_source = temp / "background.icon"
|
|
shutil.copytree(source, background_source, dirs_exist_ok=True)
|
|
document = json.loads((source / "icon.json").read_text())
|
|
document["groups"] = []
|
|
(background_source / "icon.json").write_text(json.dumps(document))
|
|
background = export(background_source, temp / "background.png")
|
|
background = background.crop((512, 0, 513, 1024)).resize((1024, 1024))
|
|
save(background, f"apps/mobile/assets/android-icon-background{suffix}.png")
|
|
save(Image.alpha_composite(background, foreground), f"apps/mobile/assets/android-icon{suffix}.png")
|
|
if variant != "production":
|
|
continue
|
|
monochrome = Image.new("RGBA", adaptive.size, "white")
|
|
monochrome.putalpha(adaptive.getchannel("A"))
|
|
save(monochrome, "apps/mobile/assets/android-icon-monochrome.png")
|
|
for name, image in [("icon-dark", dark), ("ios-icon", icon), ("ios-icon-dark", dark)]:
|
|
save(image, f"apps/mobile/assets/{name}.png")
|
|
for name, image in [("logo", icon), ("logo-dark", dark)]:
|
|
save(image, f"apps/web/public/{name}.png")
|
|
for path, size, image in [
|
|
("favicon.png", 64, icon),
|
|
("apps/mobile/assets/favicon.png", 64, icon),
|
|
("apps/web/app/favicon.png", 128, icon),
|
|
("apps/web/public/favicon.png", 128, icon),
|
|
("apps/web/public/favicon-dark.png", 128, dark),
|
|
]:
|
|
save(image, path, size)
|