Sking / gpt_skin2real_auto.py
EntropyDrop
update
73a9705
Raw
History Blame
3.94 kB
import os
import sys
import subprocess
from PIL import Image
# Import skin conversion logic
from convert_skin import convert_skin_64x32_to_64x64
def main():
skins_dir = "skins"
control_imgs_dir = "control_imgs"
tmp_dir = os.path.join(control_imgs_dir, "tmp")
front_dir = os.path.join(control_imgs_dir, "front")
back_dir = os.path.join(control_imgs_dir, "back")
if not os.path.exists(skins_dir):
print(f"Error: Skins directory '{skins_dir}' does not exist.")
sys.exit(1)
os.makedirs(tmp_dir, exist_ok=True)
os.makedirs(front_dir, exist_ok=True)
os.makedirs(back_dir, exist_ok=True)
# List all PNG files in skins
skin_files = [f for f in os.listdir(skins_dir) if f.lower().endswith(".png")]
skin_files.sort()
print(f"[*] Found {len(skin_files)} skin files in {skins_dir}.")
# 1. Pre-repair pass: check and convert all legacy 64x32 skins in the candidate directory
print("[*] Running pre-repair check on all skin files...")
for skin_file in skin_files:
skin_path = os.path.join(skins_dir, skin_file)
try:
with Image.open(skin_path) as img:
width, height = img.size
if (width, height) == (64, 32):
print(f"[*] Pre-repair: Detected legacy 64x32 skin format for {skin_file}. Auto-converting to 64x64...")
convert_skin_64x32_to_64x64(skin_path, skin_path)
except Exception as e:
print(f"[!] Pre-repair: Error checking skin size for {skin_file}: {e}")
skipped = 0
processed = 0
for skin_file in skin_files:
skin_path = os.path.join(skins_dir, skin_file)
front_path = os.path.join(front_dir, skin_file)
back_path = os.path.join(back_dir, skin_file)
tmp_output_path = os.path.join(tmp_dir, skin_file)
# Check if the output control images (front and back) already exist
if os.path.exists(front_path) and os.path.exists(back_path):
skipped += 1
continue
print("="*60)
print(f"[*] Processing: {skin_file}")
# Verify skin size (should be 64x64 after pre-repair)
try:
with Image.open(skin_path) as img:
if img.size != (64, 64):
print(f"[!] Warning: Skin '{skin_file}' has unexpected dimensions {img.size}. Skipping.")
continue
except Exception as e:
print(f"[!] Error verifying skin size: {e}")
continue
# Invoke gpt_skin2real.py using subprocess
cmd = [sys.executable, "gpt_skin2real.py", skin_path, "-o", tmp_output_path]
print(f"[*] Running command: {' '.join(cmd)}")
try:
# We run the command and check for success. If it fails, we output the error but continue to other skins.
subprocess.run(cmd, check=True)
# Split the generated image into front and back
with Image.open(tmp_output_path) as img:
width, height = img.size
left_half = img.crop((0, 0, width // 2, height))
right_half = img.crop((width // 2, 0, width, height))
left_half.save(front_path)
right_half.save(back_path)
# Clean up the temporary file in control_imgs/tmp
if os.path.exists(tmp_output_path):
os.remove(tmp_output_path)
print(f"[+] Completed: {skin_file} -> {front_path} and {back_path}")
processed += 1
except subprocess.CalledProcessError as e:
print(f"[!] Command failed for {skin_file}: {e}")
except Exception as e:
print(f"[!] Error running pipeline for {skin_file}: {e}")
print("="*60)
print(f"[*] Batch run completed. Processed: {processed}, Skipped (Already existed): {skipped}.")
if __name__ == "__main__":
main()