Moved from YourlifeisGame/Spritegenerator; projects are found via _bootstrap.py (cwd or --project) and provide config.py plus list modules. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
86 lines
3.2 KiB
Python
86 lines
3.2 KiB
Python
"""Take a generated sprite into the game.
|
|
|
|
Finds the newest raw render for <name>, keys out the black backdrop
|
|
(cutout.py), trims the transparent margin, pads to a square and scales to
|
|
SIZE px, then writes ../Assets/Sprites/<name>.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
|
|
"""
|
|
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
|
|
|
|
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):
|
|
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(raws[-1])
|
|
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:]
|
|
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) for n in args)
|
|
print(f"{ok}/{len(args)} adopted")
|