51 lines
1.9 KiB
Python
51 lines
1.9 KiB
Python
"""Print alpha bounding boxes and foot-centered anchors for generated sprite sheets."""
|
|
from pathlib import Path
|
|
from PIL import Image
|
|
|
|
|
|
SHEETS = {
|
|
"idle": ("mafuyu-idle.png", 4, 1),
|
|
"run": ("mafuyu-run.png", 4, 2),
|
|
"jump": ("mafuyu-jump.png", 4, 1),
|
|
"attack": ("mafuyu-attack.png", 4, 1),
|
|
"boss": ("mafuyu-boss.png", 4, 4),
|
|
"enemies": ("sekai-enemies.png", 4, 4),
|
|
"interactions": ("mafuyu-interactions-v2.png", 4, 4),
|
|
"interactables": ("sekai-interactables.png", 4, 4),
|
|
}
|
|
|
|
|
|
def frames(path: Path, columns: int, rows: int):
|
|
image = Image.open(path).convert("RGBA")
|
|
cell_w, cell_h = image.width // columns, image.height // rows
|
|
output = []
|
|
for row in range(rows):
|
|
for column in range(columns):
|
|
left, top = column * cell_w, row * cell_h
|
|
alpha = image.crop((left, top, left + cell_w, top + cell_h)).getchannel("A")
|
|
bbox = alpha.getbbox()
|
|
if not bbox:
|
|
raise RuntimeError(f"Empty cell {column}, {row} in {path.name}")
|
|
x0, y0, x1, y1 = bbox
|
|
padding = 6
|
|
x0, y0 = max(0, x0 - padding), max(0, y0 - padding)
|
|
x1, y1 = min(cell_w, x1 + padding), min(cell_h, y1 + padding)
|
|
source_x, source_y = left + x0, top + y0
|
|
# The model was centered in each authored cell. Keep that point as the
|
|
# horizontal body anchor even when a sword trail expands the alpha box.
|
|
anchor_x = left + cell_w / 2 - source_x
|
|
output.append({
|
|
"sx": source_x,
|
|
"sy": source_y,
|
|
"sw": x1 - x0,
|
|
"sh": y1 - y0,
|
|
"ax": round(anchor_x, 1),
|
|
"ay": y1 - y0,
|
|
})
|
|
return output
|
|
|
|
|
|
root = Path(__file__).resolve().parents[1] / "assets"
|
|
for state, (filename, columns, rows) in SHEETS.items():
|
|
print(f"{state}: {frames(root / filename, columns, rows)},")
|