Files
novafarma/scripts/master_resizer.py
2026-01-20 01:05:17 +01:00

108 lines
4.0 KiB
Python

import os
import shutil
from PIL import Image
import re
# MASTER CONFIGURATION
CONFIG = {
"teren": {
"path": "assets/slike/teren",
"size": (512, 512),
"keywords": [r"grass", r"dirt", r"soil", r"water", r"sand", r"ground", r"terrain", r"tileset", r"tree", r"rock", r"stone", r"ruin", r"house", r"building", r"wall", r"fence", r"prop", r"bush", r"flower"]
},
"liki": {
"path": "assets/slike/liki",
"size": (256, 256),
"keywords": [r"kai", r"ana", r"gronk", r"player", r"character", r"npc", r"druid", r"witch", r"farmer", r"baker", r"priest", r"guide", r"merchant", r"trader", r"kid", r"man", r"woman", r"girl", r"boy"]
},
"zombiji": {
"path": "assets/slike/animations",
"size": (128, 128),
"keywords": [r"zombie", r"undead", r"mutant", r"walk", r"run", r"idle", r"attack", r"death", r"hit", r"creature", r"rabbit", r"sheep", r"chicken", r"animal"]
},
"predmeti": {
"path": "assets/slike/predmeti",
"size": (64, 64),
"keywords": [r"tool", r"weapon", r"axe", r"pickaxe", r"vape", r"item", r"resource", r"gem", r"food", r"seed", r"crop", r"sword", r"bow", r"potion", r"rack", r"shop"]
},
"refs": {
"path": "assets/slike/MASTER_REFS",
"size": (512, 512), # NEW! Resize leftovers to 512 as default
"keywords": []
}
}
def master_process():
print("🚦 STARTING ADVANCED MASTER PROCESSOR (CLEANING REFS)...")
# 1. SCAN MASTER_REFS & DISTRIBUTE
refs_path = CONFIG["refs"]["path"]
if os.path.exists(refs_path):
print("🔍 Scanning MASTER_REFS to distribute to proper folders...")
moved_count = 0
for root, dirs, files in os.walk(refs_path):
for file in files:
if not file.lower().endswith(('.png', '.jpg', '.jpeg')):
continue
src = os.path.join(root, file)
fname_lower = file.lower()
target_cat = None
# Check all categories
for cat, data in CONFIG.items():
if cat == "refs": continue
is_match = any(re.search(p, fname_lower) for p in data["keywords"])
if is_match:
target_cat = cat
break
if target_cat:
dst_folder = CONFIG[target_cat]["path"]
dst = os.path.join(dst_folder, file)
try:
shutil.move(src, dst)
moved_count += 1
except Exception as e:
print(f"Error moving {file}: {e}")
print(f"📦 Distributed {moved_count} images from REFS to categories.")
# 2. APPLY RESIZE LOGIC (NOW INCLUDING REFS)
print("📏 Applying MASTER SIZE RULES to ALL folders...")
for category, data in CONFIG.items():
folder = data["path"]
target_size = data["size"]
if not os.path.exists(folder):
continue
print(f"🔧 Processing {category} -> {target_size}...")
count = 0
for root, dirs, files in os.walk(folder):
for file in files:
if file.lower().endswith(('.png', '.jpg', '.jpeg')):
filepath = os.path.join(root, file)
try:
with Image.open(filepath) as img:
if img.size != target_size:
img_resized = img.resize(target_size, Image.Resampling.LANCZOS)
img_resized.save(filepath)
count += 1
except Exception as e:
print(f"Error {file}: {e}")
print(f"✅ Resized {count} images in {folder}")
print("🏁 MASTER PROCESS COMPLETE.")
if __name__ == "__main__":
master_process()