Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
def extract_atlas(png_path, json_path, output_dir):
|
| 6 |
+
"""
|
| 7 |
+
Extrae sprites individuales de un atlas en formato TexturePacker,
|
| 8 |
+
usando la sección 'spritesheet.frames' del JSON.
|
| 9 |
+
|
| 10 |
+
Args:
|
| 11 |
+
png_path (str): Ruta al PNG del spritesheet.
|
| 12 |
+
json_path (str): Ruta al JSON con metadatos (debe contener 'spritesheet.frames').
|
| 13 |
+
output_dir (str): Carpeta de salida para los sprites.
|
| 14 |
+
"""
|
| 15 |
+
with open(json_path, 'r', encoding='utf-8') as f:
|
| 16 |
+
data = json.load(f)
|
| 17 |
+
|
| 18 |
+
# Acceder a la sección del spritesheet
|
| 19 |
+
if 'spritesheet' not in data or 'frames' not in data['spritesheet']:
|
| 20 |
+
raise ValueError("El JSON no contiene la estructura 'spritesheet.frames' requerida.")
|
| 21 |
+
|
| 22 |
+
frames = data['spritesheet']['frames']
|
| 23 |
+
meta = data['spritesheet'].get('meta', {})
|
| 24 |
+
image_name = meta.get('image', '')
|
| 25 |
+
|
| 26 |
+
# Cargar el spritesheet
|
| 27 |
+
spritesheet = Image.open(png_path).convert("RGBA")
|
| 28 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 29 |
+
|
| 30 |
+
extracted = 0
|
| 31 |
+
for name, info in frames.items():
|
| 32 |
+
frame = info.get('frame')
|
| 33 |
+
if not frame:
|
| 34 |
+
continue
|
| 35 |
+
|
| 36 |
+
x = frame['x']
|
| 37 |
+
y = frame['y']
|
| 38 |
+
w = frame['w']
|
| 39 |
+
h = frame['h']
|
| 40 |
+
|
| 41 |
+
# Recortar sprite
|
| 42 |
+
sprite = spritesheet.crop((x, y, x + w, y + h))
|
| 43 |
+
|
| 44 |
+
# Asegurar nombre válido
|
| 45 |
+
safe_name = "".join(c if c.isalnum() or c in "._-" else "_" for c in name)
|
| 46 |
+
if not safe_name.lower().endswith('.png'):
|
| 47 |
+
safe_name += '.png'
|
| 48 |
+
|
| 49 |
+
sprite.save(os.path.join(output_dir, safe_name))
|
| 50 |
+
extracted += 1
|
| 51 |
+
|
| 52 |
+
print(f"✅ {extracted} sprites extraídos en: {output_dir}")
|