gen-brand-assets.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. """Generate committed Android launcher and splash assets from the PC MDIOP mark."""
  2. from __future__ import annotations
  3. from pathlib import Path
  4. from PIL import Image, ImageDraw
  5. ROOT = Path(__file__).resolve().parents[2]
  6. OUTPUT_DIR = ROOT / "App" / "src" / "static" / "branding" / "android"
  7. WEB_ICON = ROOT / "Web" / "public" / "app-icons" / "mdiop-app.png"
  8. ICON_SIZES = (48, 72, 96, 144, 192, 512, 1024)
  9. def render_mdiop_icon(size: int = 1024) -> Image.Image:
  10. """Rasterize the geometry from Web/public/app-icons/brand-icon.svg."""
  11. canvas = Image.new("RGBA", (size, size), (0, 0, 0, 0))
  12. gradient = Image.new("RGBA", (size, size))
  13. pixels = gradient.load()
  14. top = (14, 165, 233)
  15. bottom = (3, 105, 161)
  16. for y in range(size):
  17. ratio = y / max(size - 1, 1)
  18. color = tuple(round(a + (b - a) * ratio) for a, b in zip(top, bottom))
  19. for x in range(size):
  20. pixels[x, y] = (*color, 255)
  21. mask = Image.new("L", (size, size), 0)
  22. ImageDraw.Draw(mask).rounded_rectangle(
  23. (0, 0, size - 1, size - 1),
  24. radius=round(size * 0.22),
  25. fill=255,
  26. )
  27. canvas.paste(gradient, (0, 0), mask)
  28. draw = ImageDraw.Draw(canvas)
  29. scale = size / 64
  30. line_width = round(4.2 * scale)
  31. m_points = [(18, 46), (18, 22), (28, 36), (38, 22), (38, 46)]
  32. m_points = [(round(x * scale), round(y * scale)) for x, y in m_points]
  33. draw.line(m_points, fill="#F8FAFC", width=line_width, joint="curve")
  34. radius = line_width // 2
  35. for x, y in m_points:
  36. draw.ellipse((x - radius, y - radius, x + radius, y + radius), fill="#F8FAFC")
  37. cyan = "#67E8F9"
  38. draw.rounded_rectangle(
  39. tuple(round(value * scale) for value in (38, 22, 50, 34)),
  40. radius=round(scale),
  41. outline=cyan,
  42. width=round(3.2 * scale),
  43. )
  44. cx, cy, circle_radius = 48 * scale, 44 * scale, 4.5 * scale
  45. draw.ellipse(
  46. (
  47. round(cx - circle_radius),
  48. round(cy - circle_radius),
  49. round(cx + circle_radius),
  50. round(cy + circle_radius),
  51. ),
  52. fill="#F8FAFC",
  53. outline=cyan,
  54. width=round(2.4 * scale),
  55. )
  56. return canvas
  57. def main() -> None:
  58. OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
  59. source = render_mdiop_icon()
  60. for size in ICON_SIZES:
  61. icon = source.resize((size, size), Image.Resampling.LANCZOS)
  62. icon.save(OUTPUT_DIR / f"icon-{size}.png", optimize=True)
  63. WEB_ICON.parent.mkdir(parents=True, exist_ok=True)
  64. source.resize((512, 512), Image.Resampling.LANCZOS).save(WEB_ICON, optimize=True)
  65. splash = Image.new("RGB", (1080, 1920), "#F8FAFC")
  66. splash_icon = source.resize((320, 320), Image.Resampling.LANCZOS)
  67. splash.paste(splash_icon, ((1080 - 320) // 2, 650), splash_icon)
  68. splash.save(OUTPUT_DIR / "splash-1080x1920.png", optimize=True)
  69. if __name__ == "__main__":
  70. main()