"""Make The Kurgan's logo and icons from the source photo, The-Kurgan-Sword.png (repo root). .venv\\Scripts\\python.exe -m kurgan.assets.make_assets [source.png] Writes: kurgan/assets/sword.png the whole sword, cleaned and trimmed (master copy) kurgan/assets/hilt.png 512 px square: the crossguard centred and as large as it fits kurgan/assets/kurgan.ico the tray icon (16-256 px), from the hilt kurgan/web/static/logo.png the web UI's header logo: the whole sword, the cut-off blade faded out kurgan/web/static/sword-vertical.png the web UI's backdrop: the whole sword, full size, turned 90° clockwise (pommel up); the page's bottom edge cuts the blade off kurgan/web/static/favicon.ico the browser tab icon, from the hilt Cleaning: the photo was cut out of a white background, leaving a pale one-pixel fringe and stair-stepped edges. The outline is pulled in by a pixel, edge pixels take the colour of the sword next to them instead of white, and the edge is smoothed to about a pixel wide. Small icons (32 px and under) have their metal brightened a little: at 16 px on a dark taskbar the mid-grey crossguard otherwise reads as black. The black grip still disappears there; an outline was tried and made the 16 px icon a blur. """ from __future__ import annotations import os import sys import numpy as np from PIL import Image, ImageFilter HERE = os.path.dirname(os.path.abspath(__file__)) ROOT = os.path.dirname(os.path.dirname(HERE)) STATIC = os.path.join(ROOT, "kurgan", "web", "static") SOURCE = os.path.join(ROOT, "The-Kurgan-Sword.png") ICON_SIZES = (16, 20, 24, 32, 40, 48, 64, 256) # tray: 16 at 100% scaling up to 32 at 200%; the rest for Windows FAVICON_SIZES = (16, 32, 48) LOGO_HEIGHT = 96 # shown 40 px high in the header: sharp up to about 240% scaling SMALL_LIFT = 1.35 # gamma applied to icons of 32 px and under BLADE_FADE = 0.18 # the last 18% of the header logo's width fades out (the photo cuts the blade off) def _bleed(rgb: np.ndarray, known: np.ndarray, steps: int) -> np.ndarray: """Give pixels outside `known` the average colour of their known neighbours, ring by ring.""" rgb, known = rgb.copy(), known.copy() for _ in range(steps): acc = np.zeros_like(rgb) count = np.zeros(known.shape, np.float32) for dy in (-1, 0, 1): for dx in (-1, 0, 1): if dy or dx: k = np.roll(known, (dy, dx), (0, 1)) acc += np.roll(rgb, (dy, dx), (0, 1)) * k[..., None] count += k new = ~known & (count > 0) rgb[new] = acc[new] / count[new][:, None] known |= new return rgb def clean(path: str) -> Image.Image: """The sword without its white fringe, with smooth edges, trimmed to its outline.""" src = np.asarray(Image.open(path).convert("RGBA")).astype(np.float32) solid = Image.fromarray(((src[..., 3] > 128) * 255).astype(np.uint8)) core_img = solid.filter(ImageFilter.MinFilter(3)) # one pixel in: drops the fringe core = np.asarray(core_img) > 0 rgb = _bleed(src[..., :3], core, 8) # edges take the sword's colour, not white soft = np.asarray(core_img.filter(ImageFilter.GaussianBlur(1.0))).astype(np.float32) / 255 alpha = np.clip((soft - 0.5) * 2.0 + 0.5, 0, 1) # smooth, about a pixel wide img = Image.fromarray(np.dstack([rgb, alpha * 255]).round().clip(0, 255).astype(np.uint8), "RGBA") return img.crop(img.getchannel("A").point(lambda v: 255 if v > 8 else 0).getbbox()) def hilt(sword: Image.Image) -> Image.Image: """A square around the crossguard (the columns where the sword is tallest), as large as fits.""" a = np.asarray(sword.getchannel("A")) > 128 guard = np.nonzero(a.sum(0) > 0.6 * a.shape[0])[0] rows = np.nonzero(a[:, guard].any(1))[0] cx, cy = (guard.min() + guard.max()) / 2, (rows.min() + rows.max()) / 2 side = round((rows.max() - rows.min() + 1) * 1.03) left, top = round(cx - side / 2), round(cy - side / 2) square = Image.new("RGBA", (side, side), (0, 0, 0, 0)) piece = sword.crop((max(left, 0), max(top, 0), min(left + side, sword.width), min(top + side, sword.height))) square.alpha_composite(piece, (max(-left, 0), max(-top, 0))) return square def icon_image(square: Image.Image, size: int) -> Image.Image: small = square.resize((size, size), Image.LANCZOS) if size > 32: return small small = small.filter(ImageFilter.UnsharpMask(0.6, 80, 0)) r, g, b, a = small.split() lift = [round(255 * (v / 255) ** (1 / SMALL_LIFT)) for v in range(256)] return Image.merge("RGBA", (r.point(lift), g.point(lift), b.point(lift), a)) def save_ico(square: Image.Image, path: str, sizes: tuple[int, ...]) -> None: images = [icon_image(square, s) for s in sizes] images[-1].save(path, format="ICO", sizes=[(s, s) for s in sizes], append_images=images[:-1]) def header_logo(sword: Image.Image) -> Image.Image: width = round(sword.width * LOGO_HEIGHT / sword.height) logo = sword.resize((width, LOGO_HEIGHT), Image.LANCZOS) x = np.linspace(0, 1, width) t = np.clip((x - (1 - BLADE_FADE)) / BLADE_FADE, 0, 1) fade = 1 - t * t * (3 - 2 * t) # smoothstep down to nothing at the tip alpha = np.asarray(logo.getchannel("A")).astype(np.float32) * fade[None, :] logo.putalpha(Image.fromarray(alpha.round().astype(np.uint8))) return logo def main(source: str = SOURCE) -> None: sword = clean(source) sword.save(os.path.join(HERE, "sword.png"), optimize=True) square = hilt(sword) square.resize((512, 512), Image.LANCZOS).save(os.path.join(HERE, "hilt.png"), optimize=True) save_ico(square, os.path.join(HERE, "kurgan.ico"), ICON_SIZES) header_logo(sword).save(os.path.join(STATIC, "logo.png"), optimize=True) sword.transpose(Image.Transpose.ROTATE_270).save(os.path.join(STATIC, "sword-vertical.png"), optimize=True) save_ico(square, os.path.join(STATIC, "favicon.ico"), FAVICON_SIZES) print(f"sword {sword.size}, hilt {square.size} -> 512, icons {ICON_SIZES}, logo height {LOGO_HEIGHT}") if __name__ == "__main__": main(*sys.argv[1:2])