Files
ComfyGen/generate_vintage.py

110 lines
3.9 KiB
Python
Raw Permalink Normal View History

"""
Vintage family tree assets (Flux.2 Klein via ComfyUI).
py -3.12 generate_vintage.py generate all (keeps existing)
py -3.12 generate_vintage.py frame --force
py -3.12 generate_vintage.py --adopt copy to ../Assets/Sprites (keyed PNGs, parchment JPG)
py -3.12 generate_vintage.py --sheet x.jpg
Frame, seal and ribbon are painted on pure black; --adopt turns black into transparency
(luma key), so the inside of the oval frame is see-through as well.
"""
import os
import sys
import generate_portraits as gp # gleiche ComfyUI-Anbindung (upload/run/graph)
import _bootstrap # Projektordner (config.py, Listen) vor allen anderen Modulen
import config
OUT = os.path.join(config.OUTPUT_DIR, "vintage")
DEST = os.path.join(config.ASSETS_DIR, "Sprites")
# Motive aus dem Projekt: config.VINTAGE_MODULE
import importlib as _il
ITEMS = _il.import_module(getattr(config, "VINTAGE_MODULE", "vintage_life")).ITEMS
def generate(name, force=False):
os.makedirs(OUT, exist_ok=True)
dest = os.path.join(OUT, name + ".png")
if os.path.exists(dest) and not force:
return
prompt, w, h, _ = ITEMS[name]
g = gp.graph(prompt, __import__("random").randint(1, 2**31), "yl_vintage/" + name)
g["8"]["inputs"].update(width=w, height=h)
g["10"]["inputs"].update(width=w, height=h)
pid = gp.post("/prompt", {"prompt": g})["prompt_id"]
import time, json, urllib.parse, urllib.request
while True:
time.sleep(1.5)
hist = gp.get(f"/history/{pid}")
if pid in hist:
st = hist[pid].get("status", {})
if st.get("status_str") == "error":
raise RuntimeError(json.dumps(st)[:1500])
if st.get("completed"):
break
for node in hist[pid]["outputs"].values():
for info in node.get("images", []):
qs = urllib.parse.urlencode({"filename": info["filename"], "subfolder": info.get("subfolder", ""), "type": info.get("type", "output")})
urllib.request.urlretrieve(f"{config.HOST}/view?{qs}", dest)
print(name, "ok", flush=True)
def luma_key(img):
"""Schwarz → durchsichtig, weicher Übergang; Farben bleiben."""
import numpy as np
from PIL import Image
a = np.asarray(img.convert("RGB")).astype("float32")
lum = a.max(axis=2)
alpha = np.clip((lum - 18) * 5.0, 0, 255)
rgba = np.dstack([a, alpha]).astype("uint8")
out = Image.fromarray(rgba, "RGBA")
return out.crop(out.getbbox() or (0, 0, out.width, out.height))
def adopt():
from PIL import Image
for name, (_, w, h, keyed) in ITEMS.items():
src = os.path.join(OUT, name + ".png")
if not os.path.exists(src):
continue
im = Image.open(src)
if keyed:
im = luma_key(im)
im.thumbnail((640, 640), Image.LANCZOS)
im.save(os.path.join(DEST, name + ".png"), optimize=True)
else:
# weißen Rand um das Pergament abschneiden
mx, my = int(im.width * 0.035), int(im.height * 0.05)
im.convert("RGB").crop((mx, my, im.width - mx, im.height - my)).resize((1920, 1080), Image.LANCZOS).save(os.path.join(DEST, name + ".jpg"), quality=88, optimize=True)
print("adopted")
def sheet(path):
from PIL import Image
tiles = []
for name in ITEMS:
f = os.path.join(OUT, name + ".png")
if os.path.exists(f):
im = Image.open(f).convert("RGB")
im.thumbnail((420, 300))
tiles.append(im)
W = sum(t.width for t in tiles) + 10 * len(tiles)
sh = Image.new("RGB", (W, 300), (60, 60, 70))
x = 0
for t in tiles:
sh.paste(t, (x, 0)); x += t.width + 10
sh.save(path, quality=88)
if __name__ == "__main__":
a = sys.argv[1:]
if "--adopt" in a:
adopt()
elif "--sheet" in a:
sheet(a[a.index("--sheet") + 1])
else:
for n in [x for x in a if x in ITEMS] or list(ITEMS):
generate(n, force="--force" in a)