#!/usr/bin/env python3 """Make LinTunes' one app icon from packaging/lint.jpg: the picture squared from its centre with its corners rounded at wave's radius (16 % of the side), as packaging/lintunes.png (desktop, 512 px) and andTunes' launcher (andtunes/app/src/main/res/drawable-nodpi/ic_launcher.png, 192 px) — the same icon on both. Re-run after changing lint.jpg; the outputs are committed, and the APK needs `andtunes/build.py --ship` to carry it. The Mac's icon is the same picture in macOS' own frame (``--only mac`` makes just that one): Apple's icon grid puts the rounded square at 824 of 1024 px — about a 10 % margin on every side — with its corners at 22.5 % of the square and a soft shadow beneath, so it sits among other Mac apps the way theirs do. ``packaging/mac/LinTunes.icns`` is written directly (the ICNS container is a header and PNG-encoded entries), so this runs on Linux and the CI never has to make it; the .icns is committed too. python3 scripts/make_icons.py # all three python3 scripts/make_icons.py --only mac # just the .icns """ import argparse import io import struct from pathlib import Path from PIL import Image, ImageDraw, ImageFilter HERE = Path(__file__).resolve().parent.parent SOURCE = HERE / "packaging" / "lint.jpg" CORNER = 0.16 # corner radius, as a fraction of the side DESKTOP = HERE / "packaging" / "lintunes.png" ANDROID = (HERE / "andtunes" / "app" / "src" / "main" / "res" / "drawable-nodpi" / "ic_launcher.png") MAC_ICNS = HERE / "packaging" / "mac" / "LinTunes.icns" # Apple's macOS icon grid (1024 px canvas). MAC_CANVAS = 1024 MAC_BODY = 824 # the rounded square; (1024-824)/2 = 100 px margin MAC_CORNER = 0.225 # of the body's side MAC_SHADOW_BLUR = 14 MAC_SHADOW_OFFSET = 10 # straight down MAC_SHADOW_ALPHA = 90 # ICNS entry types holding PNG data, by pixel size. Several sizes appear # twice: once as the 1x entry and once as the @2x of half that size. ICNS_TYPES = [ (b"icp4", 16), (b"icp5", 32), (b"ic11", 32), # 16, 32, 16@2x (b"icp6", 64), (b"ic12", 64), # 64, 32@2x (b"ic07", 128), (b"ic08", 256), (b"ic13", 256), # 128, 256, 128@2x (b"ic09", 512), (b"ic14", 512), # 512, 256@2x (b"ic10", 1024), # 512@2x ] def rounded(img: Image.Image, size: int, corner: float = CORNER) -> Image.Image: img = img.convert("RGB").resize((size, size), Image.LANCZOS) # Drawn at 4x and shrunk, so the corners are smooth rather than stepped. big = size * 4 mask = Image.new("L", (big, big), 0) ImageDraw.Draw(mask).rounded_rectangle( (0, 0, big - 1, big - 1), radius=big * corner, fill=255) out = img.convert("RGBA") out.putalpha(mask.resize((size, size), Image.LANCZOS)) return out def mac_master(src: Image.Image) -> Image.Image: """The 1024 px Mac icon: the rounded body on Apple's grid, over a soft shadow, on a transparent canvas.""" body = rounded(src, MAC_BODY, MAC_CORNER) margin = (MAC_CANVAS - MAC_BODY) // 2 shadow = Image.new("RGBA", (MAC_CANVAS, MAC_CANVAS), (0, 0, 0, 0)) silhouette = Image.new("RGBA", body.size, (0, 0, 0, MAC_SHADOW_ALPHA)) shadow.paste(silhouette, (margin, margin + MAC_SHADOW_OFFSET), body.getchannel("A")) shadow = shadow.filter(ImageFilter.GaussianBlur(MAC_SHADOW_BLUR)) shadow.alpha_composite(body, (margin, margin)) return shadow def icns_bytes(master: Image.Image) -> bytes: """An ICNS file: b'icns', the total length, then (type, length, PNG) per entry — all big-endian, each length counting its own 8 bytes.""" pngs: dict[int, bytes] = {} entries = b"" for kind, size in ICNS_TYPES: if size not in pngs: buf = io.BytesIO() master.resize((size, size), Image.LANCZOS).save( buf, format="PNG", optimize=True) pngs[size] = buf.getvalue() data = pngs[size] entries += kind + struct.pack(">I", len(data) + 8) + data return b"icns" + struct.pack(">I", len(entries) + 8) + entries def read_icns(blob: bytes) -> list[tuple[bytes, bytes]]: """[(type, payload)] of an ICNS file — for the tests.""" if blob[:4] != b"icns" or struct.unpack(">I", blob[4:8])[0] != len(blob): raise ValueError("not an ICNS file") out, pos = [], 8 while pos < len(blob): kind = blob[pos:pos + 4] length = struct.unpack(">I", blob[pos + 4:pos + 8])[0] if length < 8 or pos + length > len(blob): raise ValueError("truncated ICNS entry") out.append((kind, blob[pos + 8:pos + length])) pos += length return out def squared_source() -> Image.Image: src = Image.open(SOURCE) side = min(src.size) # square it up from the centre left, top = (src.width - side) // 2, (src.height - side) // 2 return src.crop((left, top, left + side, top + side)) def main() -> None: parser = argparse.ArgumentParser(description=__doc__.split("\n")[0]) parser.add_argument("--only", choices=("desktop", "android", "mac"), action="append", help="make only these (repeatable)") which = set(parser.parse_args().only or ("desktop", "android", "mac")) src = squared_source() wrote = [] if "desktop" in which: rounded(src, 512).save(DESKTOP) wrote.append(DESKTOP) if "android" in which: rounded(src, 192).save(ANDROID) wrote.append(ANDROID) if "mac" in which: MAC_ICNS.parent.mkdir(parents=True, exist_ok=True) MAC_ICNS.write_bytes(icns_bytes(mac_master(src))) wrote.append(MAC_ICNS) print("Wrote " + ", ".join(str(p.relative_to(HERE)) for p in wrote) + ".") if __name__ == "__main__": main()