Files
novafarma/scripts/resize_for_tiled.py
2025-12-30 23:48:51 +01:00

130 lines
3.9 KiB
Python

#!/usr/bin/env python3
"""
Resize all demo assets to proper tile sizes for Tiled editor
Converts 1024x1024 generated images to game-appropriate sizes
"""
import os
from pathlib import Path
from PIL import Image
# Resize configurations
RESIZE_CONFIG = {
'terrain': 64, # 64x64 tiles
'crops': 64, # 64x64 tiles
'buildings': 64, # 64x64 (tent is square)
'items': 32, # 32x32 for inventory items
'ui': None, # Keep original size for UI
'effects': 48, # 48x48 for effects
'environment': { # Variable sizes
'campfire': 64,
'dead_tree': (64, 96), # Tall sprite
'rock': (48, 32) # Wide sprite
},
'characters': 64, # 64x64 for character sprites
'enemies': 64 # 64x64 for enemies
}
def resize_image(input_path: Path, output_path: Path, size):
"""Resize image maintaining aspect ratio or forcing size"""
try:
img = Image.open(input_path)
if size is None:
# Keep original
if input_path != output_path:
img.save(output_path, 'PNG')
return True
if isinstance(size, tuple):
# Force exact size (width, height)
resized = img.resize(size, Image.Resampling.LANCZOS)
else:
# Square resize
resized = img.resize((size, size), Image.Resampling.LANCZOS)
resized.save(output_path, 'PNG', optimize=True)
print(f"{output_path.name}: {img.size}{resized.size}")
return True
except Exception as e:
print(f" ❌ Error with {input_path.name}: {e}")
return False
def get_target_size(category: str, filename: str):
"""Determine target size based on category and filename"""
config = RESIZE_CONFIG.get(category)
if config is None:
return None
if isinstance(config, dict):
# Environment has variable sizes
for key, size in config.items():
if key in filename:
return size
return 64 # Default for environment
return config
def resize_category(base_dir: Path, category: str):
"""Resize all images in a category folder"""
category_path = base_dir / category
if not category_path.exists():
print(f"⚠️ Skipping {category} (not found)")
return 0
print(f"\n📁 Processing: {category}/")
png_files = list(category_path.glob('*.png'))
if not png_files:
print(f" No PNG files found")
return 0
count = 0
for png_file in png_files:
size = get_target_size(category, png_file.stem)
if resize_image(png_file, png_file, size):
count += 1
return count
def main():
print("=" * 60)
print("🖼️ RESIZING DEMO ASSETS FOR TILED")
print("=" * 60)
# Resize transparent assets
demo_dir = Path('assets/images/demo')
print(f"\n📦 TRANSPARENT ASSETS: {demo_dir}")
total = 0
for category in RESIZE_CONFIG.keys():
if category != 'environment': # Handle environment specially
total += resize_category(demo_dir, category)
# Environment with variable sizes
total += resize_category(demo_dir, 'environment')
# Resize white background originals
orig_dir = Path('assets/images/demo_originals_with_white_bg')
if orig_dir.exists():
print(f"\n📦 WHITE BG ORIGINALS: {orig_dir}")
for category in RESIZE_CONFIG.keys():
if category != 'environment':
total += resize_category(orig_dir, category)
total += resize_category(orig_dir, 'environment')
print("\n" + "=" * 60)
print(f"✅ COMPLETE! Resized {total} images")
print("=" * 60)
print("\n🗺️ Ready for Tiled! All assets now proper game sizes.")
if __name__ == "__main__":
main()