Files
aetherbound-guild/tools/process_m79_region7_actors.py
T

67 lines
2.5 KiB
Python

#!/usr/bin/env python3
"""Process M79-D region-four battle art: magenta-key units to 512 squares,
battlefield to the runtime battle folder (kept at generated 16:9)."""
from __future__ import annotations
import json
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
from process_dev2_character_stickers import ( # noqa: E402
despill_key,
remove_connected_background,
)
from PIL import Image
ROOT = Path(__file__).resolve().parents[1]
GENERATION = ROOT / "docs/prototype/generation/m79_region7_actors"
OUTPUTS = GENERATION / "outputs"
RUNTIME_ENEMIES = ROOT / "runtime/assets/battle/enemies"
RUNTIME_BATTLE = ROOT / "runtime/assets/battle"
UNITS = [f"en-r7-{i:02d}" for i in range(1, 14)] + ["bo-r7-01", "bo-r7-02"]
CONTENT_FRACTION = 0.9
BOTTOM_MARGIN = 0.03
RUNTIME_SIZE = 512
def process_unit(name: str) -> dict:
src = OUTPUTS / f"battle-{name}-idle.png"
out_idle = RUNTIME_ENEMIES / f"battle-{name}-idle.png"
src_s = OUTPUTS / f"battle-{name}-strike.png"
out_strike = RUNTIME_ENEMIES / f"battle-{name}-strike.png"
for source, out in ((src, out_idle), (src_s, out_strike)):
img = Image.open(source).convert("RGBA")
img = remove_connected_background(img)
img, _changed = despill_key(img)
bbox = img.getbbox()
if bbox:
img = img.crop(bbox)
target_h = round(RUNTIME_SIZE * CONTENT_FRACTION)
scale = target_h / img.size[1]
img = img.resize((round(img.size[0] * scale), target_h), Image.LANCZOS)
canvas = Image.new("RGBA", (RUNTIME_SIZE, RUNTIME_SIZE), (0, 0, 0, 0))
x = (RUNTIME_SIZE - img.size[0]) // 2
y = RUNTIME_SIZE - img.size[1] - round(RUNTIME_SIZE * BOTTOM_MARGIN)
canvas.paste(img, (x, y), img)
canvas.save(out)
return {"source": str(src), "output": str(out_idle), "output_size": [512, 512],
"content_fraction": CONTENT_FRACTION}
def main() -> None:
items = [process_unit(n) for n in UNITS]
field = Image.open(OUTPUTS / "nightcurrent-basin-battlefield.png").convert("RGB")
field.save(RUNTIME_BATTLE / "nightcurrent_basin_battlefield.png")
report = {"items": items, "battlefield": "runtime/assets/battle/nightcurrent_basin_battlefield.png",
"battlefield_size": list(field.size)}
(GENERATION / "process_report.json").write_text(json.dumps(report, indent=1))
print("processed units=%d field=%s" % (len(items), field.size))
if __name__ == "__main__":
main()