pxg-tiny / pxg_tiny /render.py
Tarul's picture
Upload pxg_tiny/render.py with huggingface_hub
f001fd1 verified
Raw
History Blame Contribute Delete
883 Bytes
"""Sprite rendering: master-palette index grids -> RGBA -> PNG."""
import sys
from pathlib import Path
import numpy as np
from PIL import Image
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
from pxg_tiny.config import PALETTE, IMG # noqa: E402
def grid_to_rgba(grid: np.ndarray) -> np.ndarray:
"""(16,16) index grid -> (16,16,4) uint8 RGBA. Index 0 = transparent."""
out = np.zeros((IMG, IMG, 4), dtype=np.uint8)
for idx, rgba in PALETTE.items():
mask = grid == idx
out[mask] = rgba + (255,)
return out
def save_png(grid: np.ndarray, path: Path, scale: int = 8) -> Path:
rgba = grid_to_rgba(np.asarray(grid))
im = Image.fromarray(rgba, "RGBA")
im = im.resize((IMG * scale, IMG * scale), Image.NEAREST)
path = Path(path)
path.parent.mkdir(parents=True, exist_ok=True)
im.save(path)
return path