96 lines
3.8 KiB
Python
96 lines
3.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Pack independently generated cells into an exact fixed-size atlas.
|
|
|
|
This keeps cell geometry deterministic. It deliberately does not infer or
|
|
repair an irregular AI-generated grid.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
from pathlib import Path
|
|
|
|
from PIL import Image, ImageOps
|
|
|
|
|
|
def parse_size(value: str) -> tuple[int, int]:
|
|
try:
|
|
width, height = (int(part) for part in value.lower().split("x", 1))
|
|
except Exception as exc: # pragma: no cover - CLI error path
|
|
raise argparse.ArgumentTypeError("size must be WIDTHxHEIGHT") from exc
|
|
if width <= 0 or height <= 0:
|
|
raise argparse.ArgumentTypeError("size must be positive")
|
|
return width, height
|
|
|
|
|
|
def parse_rgb(value: str) -> tuple[int, int, int]:
|
|
values = tuple(int(part) for part in value.split(","))
|
|
if len(values) != 3 or any(channel < 0 or channel > 255 for channel in values):
|
|
raise argparse.ArgumentTypeError("color must be R,G,B")
|
|
return values
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--inputs", required=True, help="comma-separated source PNG paths")
|
|
parser.add_argument("--names", required=True, help="comma-separated stable cell names")
|
|
parser.add_argument("--output", required=True, type=Path)
|
|
parser.add_argument("--cols", required=True, type=int)
|
|
parser.add_argument("--cell-size", required=True, type=parse_size)
|
|
parser.add_argument("--background", type=parse_rgb, default=(255, 0, 255))
|
|
parser.add_argument("--fit", type=float, default=0.82, help="maximum source fit ratio within a cell")
|
|
args = parser.parse_args()
|
|
|
|
inputs = [Path(item) for item in args.inputs.split(",") if item]
|
|
names = [item for item in args.names.split(",") if item]
|
|
if len(inputs) != len(names):
|
|
parser.error("inputs and names must contain the same number of items")
|
|
if args.cols <= 0 or len(inputs) % args.cols:
|
|
parser.error("the number of inputs must be divisible by --cols")
|
|
if not 0 < args.fit <= 1:
|
|
parser.error("--fit must be between 0 and 1")
|
|
|
|
cell_w, cell_h = args.cell_size
|
|
rows = len(inputs) // args.cols
|
|
atlas = Image.new("RGB", (args.cols * cell_w, rows * cell_h), args.background)
|
|
entries = []
|
|
for index, (source, name) in enumerate(zip(inputs, names)):
|
|
image = Image.open(source).convert("RGBA")
|
|
max_size = (round(cell_w * args.fit), round(cell_h * args.fit))
|
|
fitted = ImageOps.contain(image, max_size, method=Image.Resampling.LANCZOS)
|
|
x = (cell_w - fitted.width) // 2
|
|
y = (cell_h - fitted.height) // 2
|
|
row, col = divmod(index, args.cols)
|
|
cell = Image.new("RGB", (cell_w, cell_h), args.background)
|
|
cell.paste(fitted, (x, y), fitted)
|
|
atlas.paste(cell, (col * cell_w, row * cell_h))
|
|
entries.append({
|
|
"name": name,
|
|
"source": str(source),
|
|
"atlas_rect": [col * cell_w, row * cell_h, cell_w, cell_h],
|
|
"source_size": list(image.size),
|
|
"fitted_size": [fitted.width, fitted.height],
|
|
"content_offset": [x, y],
|
|
})
|
|
|
|
args.output.parent.mkdir(parents=True, exist_ok=True)
|
|
atlas.save(args.output, "PNG")
|
|
manifest = args.output.with_suffix(".manifest.json")
|
|
manifest.write_text(json.dumps({
|
|
"schema": 1,
|
|
"rows": rows,
|
|
"cols": args.cols,
|
|
"cell_size": [cell_w, cell_h],
|
|
"atlas_size": list(atlas.size),
|
|
"background": list(args.background),
|
|
"fit": args.fit,
|
|
"entries": entries,
|
|
}, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
|
|
print(json.dumps({"output": str(args.output), "manifest": str(manifest), "atlas_size": list(atlas.size)}))
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|