wobblyhead hg > minor_monsters
diff tools/cut_digits.py @ 8:2c4fed8baed9
art update, persistent sound setting, tower selling as double action
| author | ark |
|---|---|
| date | 2026-10-19 13:37 +0200 |
| parents | |
| children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tools/cut_digits.py Mon Oct 19 13:37:52 2026 +0200 @@ -0,0 +1,110 @@ +""" +cut the digits out of art/digits.png and write them to art/digits/. + + python3 tools/cut_digits.py + +every connected stroke on the sheet is one character. the strokes are sorted +into the sheet's rows by their centre, and left to right within a row; the +characters of each row are listed below. a character drawn more than once +gives one variant per drawing, written as NAME_1.png, NAME_2.png and so on. +NAME is the digit, or "slash". + +all characters are scaled to the same height, the median height of the +drawings, so a number reads evenly whichever variants it uses. the paper +becomes transparent. +""" + +import statistics +from pathlib import Path + +from PIL import Image, ImageDraw + +ART = Path(__file__).resolve().parent.parent / "art" +SRC = ART / "digits.png" +OUT = ART / "digits" + +# ink is darker than this luminance +INK_LUM = 150 +# pixels fade from opaque at INK_LUM to transparent at this luminance +LUM_CLEAR = 245 +# smaller strokes are specks +MIN_PIXELS = 30 +# padding around each character, so the fade is not cut off +PAD = 3 + +# rows of the sheet, top to bottom: the lowest stroke centre of the row, and +# its characters left to right +ROWS = [ + (100, "0"), + (215, "987654321"), + (345, "0123456789"), + (492, "1/6123456789101112"), +] +NAMES = {"/": "slash"} + + +def strokes(lum): + """bounding boxes of the connected ink strokes of an L image.""" + ink = lum.point(lambda v: 255 if v < INK_LUM else 0) + w, h = ink.size + px = ink.load() + out = [] + for y in range(h): + for x in range(w): + if px[x, y] != 255: + continue + ImageDraw.floodfill(ink, (x, y), 100) + part = ink.point(lambda v: 255 if v == 100 else 0) + box = part.getbbox() + count = part.histogram()[255] + ImageDraw.floodfill(ink, (x, y), 50) + px = ink.load() + if count >= MIN_PIXELS: + out.append(box) + return out + + +def by_row(boxes): + """the boxes as one list per row of ROWS, left to right.""" + rows = [[] for _ in ROWS] + for box in boxes: + cy = (box[1] + box[3]) / 2 + row = next(i for i, (bottom, _) in enumerate(ROWS) if cy < bottom) + rows[row].append(box) + return [sorted(r) for r in rows] + + +def cut(sheet, lum, box, height): + """one character, faded out from the paper and scaled to height.""" + l, t, r, b = box + box = (max(0, l - PAD), max(0, t - PAD), min(sheet.width, r + PAD), min(sheet.height, b + PAD)) + alpha = lum.crop(box).point( + lambda v: max(0, min(255, (LUM_CLEAR - v) * 255 // (LUM_CLEAR - INK_LUM)))) + im = sheet.crop(box).convert("RGBA") + im.putalpha(alpha) + k = height / (b - t) + return im.resize((max(1, round(im.width * k)), max(1, round(im.height * k))), Image.LANCZOS) + + +def main(): + sheet = Image.open(SRC).convert("RGB") + lum = sheet.convert("L") + rows = by_row(strokes(lum)) + for (_, chars), boxes in zip(ROWS, rows): + if len(chars) != len(boxes): + raise SystemExit(f"row {chars}: found {len(boxes)} strokes, expected {len(chars)}") + height = statistics.median(b[3] - b[1] for r in rows for b in r) + OUT.mkdir(exist_ok=True) + for p in OUT.glob("*.png"): + p.unlink() + counts = {} + for (_, chars), boxes in zip(ROWS, rows): + for ch, box in zip(chars, boxes): + name = NAMES.get(ch, ch) + counts[name] = counts.get(name, 0) + 1 + cut(sheet, lum, box, height).save(OUT / f"{name}_{counts[name]}.png") + print(f"height {height}: " + ", ".join(f"{k} {v}" for k, v in sorted(counts.items()))) + + +if __name__ == "__main__": + main()
