Text-to-Image
English
numpy
machine-learning
deep-learning
generative-ai
text-2-image
image-generation
open-weights
model-weights
ai-art
pixel-art
game-development
gamedev
game-assets
asset-generator
sprite-generator
offline
tiny-model
numpy-runtime
int8-quantization
self-supervised
procedural-data
gpt
english-prompts
awesome-ai
Upload pxg_tiny/render.py with huggingface_hub
Browse files- 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
|