halkyon/tool/snap_icon.py

47 lines
1.6 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
"""Makes the snap's icon from the app's: the same picture with rounded corners.
The app's icon is a white square, which the Snap Store and the desktop's dock
show as it is, so the snap's icon rounds its corners off to transparency. The
radius, 142.222 of 512 pixels (about 28 %), is the one other desktop apps'
white rounded tiles use. The mask is drawn at four times the size and scaled
down, so the corners' edges are smooth.
Run from the repository root with Python 3 and Pillow (python3-pil on Ubuntu):
python3 tool/snap_icon.py
It rewrites snap/gui/halkyon-learn.png (512 x 512 pixels, the Store's size),
used by both the Store listing and the desktop file.
"""
from pathlib import Path
from PIL import Image, ImageDraw
SOURCE = Path("halkyon_learn_flutter/assets/icon/halkyon_icon.png")
TARGET = Path("snap/gui/halkyon-learn.png")
SIZE = 512
RADIUS = 142.222
SUPERSAMPLING = 4
def main() -> None:
icon = Image.open(SOURCE).convert("RGBA").resize((SIZE, SIZE), Image.LANCZOS)
large = SIZE * SUPERSAMPLING
mask = Image.new("L", (large, large), 0)
ImageDraw.Draw(mask).rounded_rectangle(
(0, 0, large - 1, large - 1), radius=RADIUS * SUPERSAMPLING, fill=255
)
mask = mask.resize((SIZE, SIZE), Image.LANCZOS)
# Keeps what the icon itself makes transparent, and takes the corners away.
transparent = Image.new("L", (SIZE, SIZE), 0)
icon.putalpha(Image.composite(icon.getchannel("A"), transparent, mask))
icon.save(TARGET, optimize=True)
print(f"Wrote {TARGET} ({TARGET.stat().st_size} bytes).")
if __name__ == "__main__":
main()