"""Normalize AI expansion art into fixed-anchor sixteen-frame runtime atlases. The source sheets are generated as complete 4x4 grids. This builder keeps one shared scale and anchor per animation row, motion-interpolates key poses, and packs every runtime row into sixteen isolated cells so Canvas never needs to cross-fade two full silhouettes. """ from __future__ import annotations from pathlib import Path import cv2 import numpy as np from PIL import Image, ImageDraw from build_high_frame_atlases import ( border_alpha, motion_interpolate, normalize_keyframes, pack_rows, seamless_sixteen, split_grid, ) ROOT = Path(__file__).resolve().parents[1] ASSETS = ROOT / "assets" def grid_rows(path: Path, size: tuple[int, int], padding: int) -> list[list[Image.Image]]: source = Image.open(path).convert("RGBA") cells = split_grid(source, 4, 4) return [normalize_keyframes(cells[row * 4:(row + 1) * 4], size, padding) for row in range(4)] def sequence_sixteen(keys: list[Image.Image]) -> list[Image.Image]: """Create a readable non-looping 16-frame action from four authored poses.""" output: list[Image.Image] = [] steps = (5, 5, 6) for segment, count in enumerate(steps): first, second = keys[segment], keys[segment + 1] for index in range(count): amount = index / count output.append(first if index == 0 else motion_interpolate(first, second, amount)) output[-1] = keys[-1] return output def remove_tiny_components(frame: Image.Image, min_area: int = 110) -> Image.Image: rgba = np.asarray(frame.convert("RGBA"), dtype=np.uint8).copy() count, labels, stats, _ = cv2.connectedComponentsWithStats((rgba[:, :, 3] > 12).astype(np.uint8), 8) for component in range(1, count): if stats[component, cv2.CC_STAT_AREA] < min_area: rgba[labels == component] = 0 return Image.fromarray(rgba, "RGBA") def remove_components_above_main(frame: Image.Image) -> Image.Image: """Drop shoe/hair fragments leaked from the previous generated grid row.""" rgba = np.asarray(frame.convert("RGBA"), dtype=np.uint8).copy() count, labels, stats, _ = cv2.connectedComponentsWithStats((rgba[:, :, 3] > 12).astype(np.uint8), 8) if count <= 2: return frame main = 1 + int(np.argmax(stats[1:, cv2.CC_STAT_AREA])) main_top = int(stats[main, cv2.CC_STAT_TOP]) for component in range(1, count): if component != main and int(stats[component, cv2.CC_STAT_TOP]) < main_top: rgba[labels == component] = 0 return Image.fromarray(rgba, "RGBA") def build() -> None: enemy_sources = [ "enemy-maskling-16-alpha.png", "enemy-metronome-16-alpha.png", "enemy-rain-choir-16-alpha.png", "enemy-mirror-hound-16-alpha.png", ] enemy_rows = [] for filename in enemy_sources: source = Image.open(ASSETS / filename).convert("RGBA") enemy_rows.append(normalize_keyframes(split_grid(source, 4, 4), (192, 160), 16)) pack_rows(enemy_rows, ASSETS / "sekai-expansion-enemies-16.png") prop_keys = grid_rows(ASSETS / "sekai-expansion-interactables-alpha.png", (224, 224), 18) prop_rows = [seamless_sixteen(row) for row in prop_keys] pack_rows(prop_rows, ASSETS / "sekai-expansion-interactables-16.png") boss_sources = [ "mafuyu-final-boss-idle-16-alpha.png", "mafuyu-final-boss-cast-16-alpha.png", "mafuyu-final-boss-dash-16-alpha.png", "mafuyu-final-boss-defeat-16-alpha.png", ] boss_rows = [] for row, filename in enumerate(boss_sources): source = Image.open(ASSETS / filename).convert("RGBA") raw = split_grid(source, 4, 4) if row == 1: raw = [raw[index] for index in [*range(12), 8, 5, 2, 0]] frames = [remove_components_above_main(frame) for frame in normalize_keyframes(raw, (256, 256), 18)] boss_rows.append([remove_tiny_components(frame) if row == 0 else frame for frame in frames]) pack_rows(boss_rows, ASSETS / "mafuyu-final-boss-16.png") for label, rows in (("enemy", enemy_rows), ("prop", prop_rows), ("boss", boss_rows)): if any(border_alpha(frame) > 12 for row in rows for frame in row): raise RuntimeError(f"{label} expansion atlas touches a frame border") preview = Image.new("RGBA", (1120, 2400), (10, 10, 24, 255)) draw = ImageDraw.Draw(preview) groups = [ ("NEW SEKAI ENEMIES", enemy_rows, 150), ("NEW INTERACTABLES", prop_rows, 170), ("THE UNWRITTEN MAFUYU", boss_rows, 180), ] y = 18 for label, rows, thumb_size in groups: draw.text((20, y), label, fill=(220, 211, 255, 255)) y += 28 for row in rows: for column, index in enumerate((0, 3, 6, 9, 12, 15)): frame = row[index].copy() frame.thumbnail((thumb_size, thumb_size), Image.Resampling.LANCZOS) x = 18 + column * 182 + (thumb_size - frame.width) // 2 preview.alpha_composite(frame, (x, y + thumb_size - frame.height)) y += thumb_size + 8 y += 20 preview.crop((0, 0, preview.width, min(preview.height, y))).save( ASSETS / "expansion-animation-preview.png", optimize=True ) print("Built expansion enemy, prop, and final-boss 16-frame atlases.") if __name__ == "__main__": build()