wobblyhead hg > minor_monsters
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 7:4fd2da999ba4 | 8:2c4fed8baed9 |
|---|---|
| 1 """ | |
| 2 cut the digits out of art/digits.png and write them to art/digits/. | |
| 3 | |
| 4 python3 tools/cut_digits.py | |
| 5 | |
| 6 every connected stroke on the sheet is one character. the strokes are sorted | |
| 7 into the sheet's rows by their centre, and left to right within a row; the | |
| 8 characters of each row are listed below. a character drawn more than once | |
| 9 gives one variant per drawing, written as NAME_1.png, NAME_2.png and so on. | |
| 10 NAME is the digit, or "slash". | |
| 11 | |
| 12 all characters are scaled to the same height, the median height of the | |
| 13 drawings, so a number reads evenly whichever variants it uses. the paper | |
| 14 becomes transparent. | |
| 15 """ | |
| 16 | |
| 17 import statistics | |
| 18 from pathlib import Path | |
| 19 | |
| 20 from PIL import Image, ImageDraw | |
| 21 | |
| 22 ART = Path(__file__).resolve().parent.parent / "art" | |
| 23 SRC = ART / "digits.png" | |
| 24 OUT = ART / "digits" | |
| 25 | |
| 26 # ink is darker than this luminance | |
| 27 INK_LUM = 150 | |
| 28 # pixels fade from opaque at INK_LUM to transparent at this luminance | |
| 29 LUM_CLEAR = 245 | |
| 30 # smaller strokes are specks | |
| 31 MIN_PIXELS = 30 | |
| 32 # padding around each character, so the fade is not cut off | |
| 33 PAD = 3 | |
| 34 | |
| 35 # rows of the sheet, top to bottom: the lowest stroke centre of the row, and | |
| 36 # its characters left to right | |
| 37 ROWS = [ | |
| 38 (100, "0"), | |
| 39 (215, "987654321"), | |
| 40 (345, "0123456789"), | |
| 41 (492, "1/6123456789101112"), | |
| 42 ] | |
| 43 NAMES = {"/": "slash"} | |
| 44 | |
| 45 | |
| 46 def strokes(lum): | |
| 47 """bounding boxes of the connected ink strokes of an L image.""" | |
| 48 ink = lum.point(lambda v: 255 if v < INK_LUM else 0) | |
| 49 w, h = ink.size | |
| 50 px = ink.load() | |
| 51 out = [] | |
| 52 for y in range(h): | |
| 53 for x in range(w): | |
| 54 if px[x, y] != 255: | |
| 55 continue | |
| 56 ImageDraw.floodfill(ink, (x, y), 100) | |
| 57 part = ink.point(lambda v: 255 if v == 100 else 0) | |
| 58 box = part.getbbox() | |
| 59 count = part.histogram()[255] | |
| 60 ImageDraw.floodfill(ink, (x, y), 50) | |
| 61 px = ink.load() | |
| 62 if count >= MIN_PIXELS: | |
| 63 out.append(box) | |
| 64 return out | |
| 65 | |
| 66 | |
| 67 def by_row(boxes): | |
| 68 """the boxes as one list per row of ROWS, left to right.""" | |
| 69 rows = [[] for _ in ROWS] | |
| 70 for box in boxes: | |
| 71 cy = (box[1] + box[3]) / 2 | |
| 72 row = next(i for i, (bottom, _) in enumerate(ROWS) if cy < bottom) | |
| 73 rows[row].append(box) | |
| 74 return [sorted(r) for r in rows] | |
| 75 | |
| 76 | |
| 77 def cut(sheet, lum, box, height): | |
| 78 """one character, faded out from the paper and scaled to height.""" | |
| 79 l, t, r, b = box | |
| 80 box = (max(0, l - PAD), max(0, t - PAD), min(sheet.width, r + PAD), min(sheet.height, b + PAD)) | |
| 81 alpha = lum.crop(box).point( | |
| 82 lambda v: max(0, min(255, (LUM_CLEAR - v) * 255 // (LUM_CLEAR - INK_LUM)))) | |
| 83 im = sheet.crop(box).convert("RGBA") | |
| 84 im.putalpha(alpha) | |
| 85 k = height / (b - t) | |
| 86 return im.resize((max(1, round(im.width * k)), max(1, round(im.height * k))), Image.LANCZOS) | |
| 87 | |
| 88 | |
| 89 def main(): | |
| 90 sheet = Image.open(SRC).convert("RGB") | |
| 91 lum = sheet.convert("L") | |
| 92 rows = by_row(strokes(lum)) | |
| 93 for (_, chars), boxes in zip(ROWS, rows): | |
| 94 if len(chars) != len(boxes): | |
| 95 raise SystemExit(f"row {chars}: found {len(boxes)} strokes, expected {len(chars)}") | |
| 96 height = statistics.median(b[3] - b[1] for r in rows for b in r) | |
| 97 OUT.mkdir(exist_ok=True) | |
| 98 for p in OUT.glob("*.png"): | |
| 99 p.unlink() | |
| 100 counts = {} | |
| 101 for (_, chars), boxes in zip(ROWS, rows): | |
| 102 for ch, box in zip(chars, boxes): | |
| 103 name = NAMES.get(ch, ch) | |
| 104 counts[name] = counts.get(name, 0) + 1 | |
| 105 cut(sheet, lum, box, height).save(OUT / f"{name}_{counts[name]}.png") | |
| 106 print(f"height {height}: " + ", ".join(f"{k} {v}" for k, v in sorted(counts.items()))) | |
| 107 | |
| 108 | |
| 109 if __name__ == "__main__": | |
| 110 main() |
