MiataDrive: sprite sheet cleanup pass

sprite_tool.py gains a 'clean' subcommand that tidies the sheet in place:
drops specks (<=4px) and 1px sliver lines left over from the original
checker-keyed extractions, and re-centers every sprite in its cell
(bottom-anchored, 2px margin) so nothing touches the cell edges.
Tree trunks / palm fronds are protected from thin-line removal.

Run on the sheet: 489 junk pixels removed; re-import crops tightened
(e.g. car_white_turn 108x60 -> 92x54, car_yellow_turn 86x60 -> 80x56).
This commit is contained in:
Dejvino
2026-11-12 22:34:22 +01:00
parent 2ac8706606
commit 1a1ab8637e
3 changed files with 1516 additions and 1672 deletions
+84
View File
@@ -25,6 +25,7 @@ Requires Pillow: python3 -m pip install --user pillow
import re
import sys
import os
from collections import deque
try:
from PIL import Image, ImageDraw
@@ -182,12 +183,95 @@ struct GameSprite {
print(f"wrote {HDR}")
def _cell_components(cell):
"""8-connected components of non-background pixels -> list of (area, minx, miny, maxx, maxy, pixelset)"""
w, h = cell.size
px = cell.load()
seen = [[False] * w for _ in range(h)]
comps = []
for y in range(h):
for x in range(w):
if is_bg(px[x, y]) or seen[y][x]:
continue
q = deque([(x, y)])
seen[y][x] = True
area = 0
minx = maxx = x
miny = maxy = y
pix = set()
while q:
a, b = q.popleft()
area += 1
pix.add((a, b))
if a < minx: minx = a
if a > maxx: maxx = a
if b < miny: miny = b
if b > maxy: maxy = b
for dx in (-1, 0, 1):
for dy in (-1, 0, 1):
nx, ny = a + dx, b + dy
if 0 <= nx < w and 0 <= ny < h and not seen[ny][nx] and not is_bg(px[nx, ny]):
seen[ny][nx] = True
q.append((nx, ny))
comps.append((area, minx, miny, maxx, maxy, pix))
comps.sort(reverse=True)
return comps
# cells whose 1px-wide vertical runs may be real content (tree trunks etc.)
NO_THIN_REMOVAL = {"palm", "pine"}
def cmd_clean():
"""tidy sprites_sheet.png in place: drop specks/1px slivers, re-center sprites"""
sheet = Image.open(SHEET).convert("RGB")
removed_total = 0
for i, name in enumerate(NAMES):
cx, cy = (i % COLS) * CELL_W, (i // COLS) * CELL_H
cell = sheet.crop((cx, cy + LABEL_H, cx + CELL_W, cy + CELL_H))
w, h = cell.size
comps = _cell_components(cell)
if not comps:
continue
allow_thin = name not in NO_THIN_REMOVAL
removed = set()
for area, minx, miny, maxx, maxy, pix in comps[1:]:
bw, bh = maxx - minx + 1, maxy - miny + 1
tiny = area <= 4
sliver = allow_thin and ((bh == 1 and bw <= 30) or (bw == 1 and bh <= 30))
if tiny or sliver:
removed |= pix
if removed:
px = cell.load()
for x, y in removed:
px[x, y] = MAGENTA
removed_total += len(removed)
# re-center: horizontal middle, bottom anchored 2px above content bottom
comps = _cell_components(cell)
minx = min(c[1] for c in comps)
maxx = max(c[3] for c in comps)
miny = min(c[2] for c in comps)
maxy = max(c[4] for c in comps)
bw, bh = maxx - minx + 1, maxy - miny + 1
dx = (CELL_W - bw) // 2 - minx
dy = (h - 2 - maxy)
if dx or dy:
shifted = Image.new("RGB", (w, h), MAGENTA)
shifted.paste(cell, (dx, dy))
sheet.paste(shifted, (cx, cy + LABEL_H))
sheet.save(SHEET)
print(f"cleaned {SHEET}: {removed_total} junk pixels removed, sprites re-centered")
if __name__ == "__main__":
mode = sys.argv[1] if len(sys.argv) > 1 else ""
if mode == "export":
cmd_export()
elif mode == "import":
cmd_import()
elif mode == "clean":
cmd_clean()
else:
print(__doc__)
sys.exit(1)
+1432 -1672
View File
File diff suppressed because it is too large Load Diff
Binary file not shown.

Before

Width:  |  Height:  |  Size: 144 KiB

After

Width:  |  Height:  |  Size: 143 KiB