Generating app assets with Python scripts (logos, GIFs, sounds)
pythonpillownumpyffmpeggifwebpwavassetsicons
Justin's rule is that app assets (icons, logos, sounds) are produced by a committed script so they can be rebuilt. The desktop has Pillow and numpy but no system ffmpeg, sox or OpenCV (a standalone ffmpeg lives at ~/bin/ffmpeg). Techniques that worked:
Keying a logo out of a white background
- To remove the white background but keep white areas inside the artwork (white letters on keycaps), flood-fill from the image corners and only make the filled region transparent.
Image.fromarray(array)returns an image backed by a read-only buffer;ImageDraw.floodfillon it silently does nothing. Call.copy()first. This shipped an animated space-bar logo with an opaque white box around the keycaps (Keyboard v1.18.0, fixed in v1.19.1).- To split a wordmark into parts automatically, find the widest run of empty columns.
- For a part that should follow the theme (black lettering on a light keyboard, white on dark), keep only its alpha (
extractAlpha()) and tint it at draw time, or use a ColorMatrix that maps black to the theme's text color while leaving colored accents alone.
Deriving a dark variant of a light-background graphic
Un-multiply each pixel against white to recover color plus alpha (the inverse of compositing over white), composite onto the dark background (#121212), flip the lightness of black lettering, and keep opaque parts opaque via a hole-filled silhouette.
Video to GIF or frames
- GIF: dump frames with ffmpeg, process them with numpy/PIL, then reassemble with ffmpeg's
palettegen+paletteusetwo-pass for good colors. A 448x168, 145-frame, 24 fps loop came out at about 1.3 MB. - In-app animation: pack lossy WebP frames into one asset file (73 frames at 331x84, 12 fps, about 300 KB) and decode into one reused bitmap; only animate while the view is attached and drawn, and show frame 0 when system animations are off.
- When no video tool is available, a poster frame (
poster.jpg) is a fine source for a still logo. - Strip the audio track from background videos for the web; it saves bytes and avoids autoplay blocks.
UI sounds without audio tools
Python's stdlib wave + math is enough for short UI sounds: a send "whoosh" as a 620 to 1250 Hz eased sweep over 130 ms, a receive "plink" as E5 to B5 over 170 ms, both around -12 dBFS with short fades. Commit the generator; regenerate to retune.
Preparing downloaded photos (Wikimedia Commons and similar)
A site that uses free-licensed photos needs a small, repeatable pipeline: a metadata file with
one entry per photo (source page, author, license, license URL), the originals in a gitignored
folder, and a script that produces the resized web versions. The metadata also feeds a credits
page, which CC BY and CC BY-SA licenses require anyway.
- Apply the EXIF orientation before resizing: img = ImageOps.exif_transpose(img). Pillow
ignores the orientation tag on load, and most pipelines strip metadata on save, so a photo
taken with the camera rotated comes out sideways or upside down with nothing left to tell
the browser to fix it.
- Download the original file, not a thumbnail URL at a chosen width. Commons thumbnail URLs
now serve only a fixed set of widths; a request for 2400 px silently returned a 960 px image.
Take the original url from the API or the file page and resize locally, then check the
dimensions of what actually arrived.
- Verify what a photo shows, not just its title. Files are named and categorized by
uploaders. A set of pier photos labeled South Padre Island actually showed a pier on North
Padre Island near Corpus Christi, more than 100 miles up the coast. For a site about a specific place,
compare against a map or a second source before publishing.
- Check the file page for a deletion or copyright notice before using it, and prefer versions
without watermarks.
See also
- Writing an Android keyboard (InputMethodService)
- Encoding phone clips for the web (HEVC remux, H.264 fallback)