"""Take a generated sprite into the game. Finds the newest raw render for , keys out the black backdrop (cutout.py), trims the transparent margin, pads to a square and scales to SIZE px, then writes ../Assets/Sprites/.png. The game draws sprites with PictureBoxSizeMode.Zoom, so square keeps every icon the same visual size. py -3.12 adopt.py stat_health one sprite py -3.12 adopt.py stat_health life_baby several py -3.12 adopt.py --all every name in the prompt list py -3.12 adopt.py --size 512 hero larger square (default 128) py -3.12 adopt.py --holes archer also clear enclosed black (see cutout.py) py -3.12 adopt.py --ai hero dark parts decided by a matting model (rembg) """ import glob import importlib import os import sys from PIL import Image import _bootstrap # Projektordner (config.py, Listen) vor allen anderen Modulen import config from cutout import cutout, cutout_ai HERE = os.path.dirname(os.path.abspath(__file__)) DEST = os.path.join(config.ASSETS_DIR, "Sprites") SIZE = 128 MARGIN = 0.04 # share of the side left free around the icon ALPHA_THRESHOLD = 8 def adopt(name, size=None, holes=False, ai=False): size = size or SIZE raws = sorted(glob.glob(os.path.join(config.OUTPUT_DIR, f"{name}_raw_*.png")), key=os.path.getmtime) if not raws: print(f" {name}: no raw render in output/, skipped") return False image = cutout_ai(raws[-1]) if ai else cutout(raws[-1], holes=holes) box = image.getchannel("A").point(lambda v: 255 if v > ALPHA_THRESHOLD else 0).getbbox() if box is None: print(f" {name}: fully transparent after cutout, skipped") return False image = image.crop(box) side = int(max(image.size) * (1 + 2 * MARGIN)) square = Image.new("RGBA", (side, side), (0, 0, 0, 0)) square.paste(image, ((side - image.width) // 2, (side - image.height) // 2)) square = square.resize((size, size), Image.LANCZOS) os.makedirs(DEST, exist_ok=True) dest = os.path.join(DEST, f"{name}.png") square.save(dest, optimize=True) print(f" {name:18s} {os.path.basename(raws[-1])} -> Assets/Sprites/{name}.png") return True def adopt_backdrop(name, width=1920, height=1080): """Full-screen picture: no cutout, cover-crop to 16:9, saved as JPEG.""" raws = sorted(glob.glob(os.path.join(config.OUTPUT_DIR, f"{name}_raw_*.png")), key=os.path.getmtime) if not raws: print(f" {name}: no raw render in output/, skipped") return False image = Image.open(raws[-1]).convert("RGB") scale = max(width / image.width, height / image.height) image = image.resize((round(image.width * scale), round(image.height * scale)), Image.LANCZOS) left, top = (image.width - width) // 2, (image.height - height) // 2 image = image.crop((left, top, left + width, top + height)) dest = os.path.join(DEST, f"{name}.jpg") image.save(dest, quality=88, optimize=True) print(f" {name:18s} {os.path.basename(raws[-1])} -> Assets/Sprites/{name}.jpg") return True if __name__ == "__main__": args = sys.argv[1:] size = None holes = "--holes" in args ai = "--ai" in args args = [a for a in args if a not in ("--holes", "--ai")] if len(args) >= 2 and args[0] == "--size": size = int(args[1]) args = args[2:] if not args: print(__doc__) sys.exit(1) if args[0] == "--backdrop": sys.exit(0 if all(adopt_backdrop(n) for n in args[1:]) else 1) if args == ["--all"]: prompts = importlib.import_module(getattr(config, "PROMPTS_MODULE", "prompts_life")) args = [p[0] for p in prompts.PROMPTS] ok = sum(adopt(n, size, holes, ai) for n in args) print(f"{ok}/{len(args)} adopted")