Tarul commited on
Commit
f001fd1
·
verified ·
1 Parent(s): 1f602a8

Upload pxg_tiny/render.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. pxg_tiny/render.py +28 -0
pxg_tiny/render.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Sprite rendering: master-palette index grids -> RGBA -> PNG."""
2
+ import sys
3
+ from pathlib import Path
4
+
5
+ import numpy as np
6
+ from PIL import Image
7
+
8
+ sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
9
+ from pxg_tiny.config import PALETTE, IMG # noqa: E402
10
+
11
+
12
+ def grid_to_rgba(grid: np.ndarray) -> np.ndarray:
13
+ """(16,16) index grid -> (16,16,4) uint8 RGBA. Index 0 = transparent."""
14
+ out = np.zeros((IMG, IMG, 4), dtype=np.uint8)
15
+ for idx, rgba in PALETTE.items():
16
+ mask = grid == idx
17
+ out[mask] = rgba + (255,)
18
+ return out
19
+
20
+
21
+ def save_png(grid: np.ndarray, path: Path, scale: int = 8) -> Path:
22
+ rgba = grid_to_rgba(np.asarray(grid))
23
+ im = Image.fromarray(rgba, "RGBA")
24
+ im = im.resize((IMG * scale, IMG * scale), Image.NEAREST)
25
+ path = Path(path)
26
+ path.parent.mkdir(parents=True, exist_ok=True)
27
+ im.save(path)
28
+ return path