unis-oa/backend/scripts/gen_portal_covers.py

119 lines
4.7 KiB
Python

#!/usr/bin/env python3
"""Regenerate the seeded homepage cover images.
python3 scripts/gen_portal_covers.py
Writes flat editorial PNGs into ``assets/portal_covers``; ``app.seed.stage_cover``
copies them into the upload storage. The portal headline panel crops to a short
horizontal band and paints its own caption over the bottom, so the artwork keeps
every mark inside the vertical middle band and carries no article title.
"""
from __future__ import annotations
import sys
from pathlib import Path
from PIL import Image, ImageDraw, ImageFont
WIDTH, HEIGHT = 720, 360
# The portal headline panel crops to a short band and paints its caption over the
# lower two thirds, so marks live in the thin strip that stays unobstructed.
SAFE_TOP, SAFE_BOTTOM = 90, 270
CLEAR_TOP, CLEAR_BOTTOM = 92, 146
ASSET_DIR = Path(__file__).resolve().parents[1] / "assets" / "portal_covers"
HEI = ("/System/Library/Fonts/Hiragino Sans GB.ttc", 2)
# file, tag, colours (top, bottom), accent, motif
COVERS = [
("notice-holiday.png", "通知公告", "#0f766e", "#042f2e", "#5eead4", "bars"),
("notice-unisoa.png", "通知公告", "#1e293b", "#020617", "#2dd4bf", "flow"),
("news-tech-review.png", "新闻动态", "#b45309", "#451a03", "#fcd34d", "rings"),
("news-channel-show.png", "新闻动态", "#155e75", "#083344", "#67e8f9", "grid"),
]
def rgb(value: str) -> tuple[int, int, int]:
value = value.lstrip("#")
return int(value[0:2], 16), int(value[2:4], 16), int(value[4:6], 16)
def font(size: int) -> ImageFont.FreeTypeFont:
try:
return ImageFont.truetype(HEI[0], size, index=HEI[1])
except OSError: # pragma: no cover - non-macos fallback
return ImageFont.load_default()
def gradient(top: str, bottom: str) -> Image.Image:
img = Image.new("RGB", (WIDTH, HEIGHT), top)
draw = ImageDraw.Draw(img)
a, b = rgb(top), rgb(bottom)
for y in range(HEIGHT):
ratio = y / (HEIGHT - 1)
draw.line([(0, y), (WIDTH, y)], fill=tuple(round(x + (y2 - x) * ratio) for x, y2 in zip(a, b)))
return img
def motif(layer: ImageDraw.ImageDraw, kind: str, accent: tuple[int, int, int]) -> None:
"""Right-hand ornament; the caption only sits over the left half."""
tint = accent + (70,)
mid = (CLEAR_TOP + SAFE_BOTTOM) // 2
if kind == "bars":
for i, height in enumerate((46, 88, 62, 116, 74, 100, 54)):
layer.rounded_rectangle(
[WIDTH - 300 + i * 42, SAFE_BOTTOM - height, WIDTH - 300 + i * 42 + 26, SAFE_BOTTOM],
radius=6,
fill=tint,
)
elif kind == "flow":
points = [(WIDTH - 320 + i * 92, mid - 46 + (i % 2) * 56) for i in range(4)]
layer.line(points, fill=tint, width=4, joint="curve")
for x, y in points:
layer.ellipse([x - 12, y - 12, x + 12, y + 12], fill=accent + (95,), outline=(255, 255, 255, 120), width=2)
elif kind == "rings":
for radius in range(34, 190, 30):
layer.arc(
[WIDTH - 200 - radius, mid - radius, WIDTH - 200 + radius, mid + radius],
start=195, end=355, fill=tint, width=5,
)
else:
for x in range(WIDTH - 280, WIDTH + 40, 40):
layer.line([(x, CLEAR_TOP), (x - 70, SAFE_BOTTOM)], fill=accent + (40,), width=2)
for y in range(CLEAR_TOP, SAFE_BOTTOM + 1, 32):
layer.line([(WIDTH - 280, y), (WIDTH, y)], fill=accent + (26,), width=2)
def render(spec: tuple[str, str, str, str, str, str, str]) -> Path:
name, tag, top, bottom, accent, kind = spec
base = gradient(top, bottom).convert("RGBA")
overlay = Image.new("RGBA", (WIDTH, HEIGHT), (0, 0, 0, 0))
draw = ImageDraw.Draw(overlay)
motif(draw, kind, rgb(accent))
tag_font, mark_font = font(21), font(30)
# ImageDraw writes raw pixels on RGBA targets, so translucent shapes live on the
# overlay and get composited once. Tag and wordmark share one line because only
# the top of the crop is free of the panel caption.
tag_w = draw.textlength(tag, font=tag_font)
draw.rounded_rectangle([52, CLEAR_TOP + 2, 52 + tag_w + 32, CLEAR_TOP + 42], radius=20, fill=rgb(accent) + (60,))
draw.text((52 + 16, CLEAR_TOP + 9), tag, font=tag_font, fill=(255, 255, 255, 235))
mark_x = 52 + tag_w + 32 + 22
draw.text((mark_x, CLEAR_TOP + 4), "UnisOA", font=mark_font, fill=(255, 255, 255, 205))
draw.rounded_rectangle([44, CLEAR_TOP + 2, 49, CLEAR_BOTTOM - 6], radius=3, fill=rgb(accent) + (230,))
base.alpha_composite(overlay)
target = ASSET_DIR / name
base.convert("RGB").save(target, "PNG", optimize=True)
return target
def main() -> int:
ASSET_DIR.mkdir(parents=True, exist_ok=True)
for spec in COVERS:
print("wrote", render(spec))
return 0
if __name__ == "__main__":
sys.exit(main())