Files
ComfyGen/adopt_scene.py
2026-11-13 00:57:30 +01:00

70 lines
2.5 KiB
Python

"""Take a generated scene (background, title art, splash) into the game WITHOUT cutting it out.
Finds the newest raw render for <name> in the project's output/, scales it (cover: fills the
target size, crops the overflow from the centre) and writes ASSETS_DIR/Sprites/<name>.jpg.
Sprites with a black backdrop go through adopt.py instead.
py -3.12 adopt_scene.py bg_title one scene, keeps the render size
py -3.12 adopt_scene.py bg_title bg_shop several
py -3.12 adopt_scene.py --size 1600x900 bg_title target size (cover + centre crop)
py -3.12 adopt_scene.py --quality 80 bg_title JPEG quality (default 85)
py -3.12 adopt_scene.py --png bg_title PNG instead of JPEG
"""
import glob
import os
import sys
from PIL import Image
import _bootstrap # project folder (config.py, lists) before every other module
import config
DEST = os.path.join(config.ASSETS_DIR, "Sprites")
def adopt_scene(name, size=None, quality=85, png=False):
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
img = Image.open(raws[-1]).convert("RGB")
if size:
tw, th = size
k = max(tw / img.width, th / img.height)
img = img.resize((max(tw, round(img.width * k)), max(th, round(img.height * k))), Image.LANCZOS)
left, top = (img.width - tw) // 2, (img.height - th) // 2
img = img.crop((left, top, left + tw, top + th))
os.makedirs(DEST, exist_ok=True)
out = os.path.join(DEST, name + (".png" if png else ".jpg"))
if png:
img.save(out, optimize=True)
else:
img.save(out, quality=quality, optimize=True, progressive=True)
print(f" {name}: {os.path.basename(raws[-1])} -> {out} ({img.width}x{img.height}, {os.path.getsize(out) // 1024} KB)")
return True
def main(argv):
size, quality, png, names = None, 85, False, []
i = 0
while i < len(argv):
a = argv[i]
if a == "--size":
w, h = argv[i + 1].lower().split("x"); size = (int(w), int(h)); i += 2; continue
if a == "--quality":
quality = int(argv[i + 1]); i += 2; continue
if a == "--png":
png = True
else:
names.append(a)
i += 1
if not names:
print(__doc__)
return 1
ok = all([adopt_scene(n, size, quality, png) for n in names])
return 0 if ok else 1
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))