wobblyhead hg > minor_monsters
annotate tools/cut_sheet.py @ 29:0541f1b5ce7b
splash screen config update
| author | ark |
|---|---|
| date | 2026-10-20 17:54 +0200 |
| parents | 2c4fed8baed9 |
| children |
| rev | line source |
|---|---|
| 2 | 1 """ |
| 2 cut the sprites out of a scanned drawing sheet and write them to art/. | |
| 3 | |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
2
diff
changeset
|
4 python3 tools/cut_sheet.py art/art_sources/source_sheet_2.jpg |
| 2 | 5 |
| 6 the boxes below are set by hand for source_sheet_2.jpg, in sheet pixels, at | |
| 7 the sheet's own scale. each box holds one drawing with an inked outline. the | |
| 8 largest connected outline in a box is the drawing; other ink in the box, such | |
| 9 as a neighbour's edge, is dropped. the inside of the outline keeps the | |
| 10 scanned colour, the paper is whitened, the outside becomes transparent. | |
| 11 | |
| 12 the t*p strips are made of four boxes each, levels 2-5, laid out side by side | |
| 13 with empty columns between them, as the build expects (see images.py). | |
| 14 | |
| 15 coins and maps are not on the sheet in inked form and are not written. | |
| 16 """ | |
| 17 | |
| 18 import sys | |
| 19 from pathlib import Path | |
| 20 | |
| 21 from PIL import Image, ImageDraw, ImageFilter | |
| 22 | |
| 23 ART = Path(__file__).resolve().parent.parent / "art" | |
| 24 | |
| 25 # box padding, so the outline never touches the crop border | |
| 26 PAD = 12 | |
| 27 # outline ink is darker than this luminance | |
| 28 INK_LUM = 150 | |
| 29 # outside the outline, pixels fade from opaque at INK_LUM to transparent here | |
| 30 LUM_CLEAR = 245 | |
| 31 # paper is lighter than this and becomes white; darker colours are scaled | |
| 32 PAPER_LUM = 235 | |
| 33 # other outlines in a box are kept if they reach this share of the largest | |
| 34 # and do not touch the box border | |
| 35 KEEP_SHARE = 0.05 | |
| 36 # empty columns between the levels of a tower strip | |
| 37 STRIP_GAP = 40 | |
| 38 | |
| 39 SPRITES = { | |
| 40 "hpm": (284, 204, 424, 260), | |
| 41 "m0": (280, 280, 428, 412), | |
| 42 "m1": (496, 264, 628, 388), | |
| 43 "m2": (676, 276, 780, 376), | |
| 44 "m3": (804, 248, 916, 364), | |
| 45 "m4": (924, 244, 1040, 368), | |
| 46 "t1": (236, 564, 436, 696), | |
| 47 "t0": (476, 528, 632, 684), | |
| 48 "t2": (688, 512, 816, 668), | |
| 49 "t3": (872, 500, 1040, 652), | |
| 50 "hpb": (172, 730, 450, 785), | |
| 51 "b0": (200, 780, 440, 950), | |
| 52 "b1": (436, 760, 648, 950), | |
| 53 "b2": (688, 700, 828, 904), | |
| 54 "b3": (896, 712, 1040, 896), | |
| 55 "p1": (236, 1044, 300, 1096), | |
| 56 "p0": (520, 980, 568, 1080), | |
| 57 "p2": (720, 1008, 784, 1076), | |
| 58 "p3": (920, 976, 996, 1052), | |
| 59 } | |
| 60 | |
| 61 STRIPS = { | |
| 62 "t1p": [(1284, 464, 1508, 600), (1552, 424, 1808, 592), | |
| 63 (1816, 404, 2044, 560), (2080, 392, 2456, 548)], | |
| 64 "t0p": [(2544, 360, 2712, 540), (2736, 340, 2960, 548), | |
| 65 (2980, 300, 3226, 572), (3226, 290, 3500, 580)], | |
| 66 "t2p": [(1320, 672, 1452, 840), (1536, 656, 1648, 828), | |
| 67 (1720, 616, 1856, 812), (1920, 592, 2064, 796)], | |
| 68 "t3p": [(2120, 596, 2292, 788), (2312, 616, 2560, 804), | |
| 69 (2580, 620, 2800, 800), (2816, 552, 3036, 788)], | |
| 70 } | |
| 71 | |
| 72 | |
| 73 def outlines(walls): | |
| 74 """ | |
| 75 return the connected parts of a mask as (count, seed, touches border), | |
| 76 largest first. walls is an L image with 255 for ink. | |
| 77 """ | |
| 78 w, h = walls.size | |
| 79 work = walls.copy() | |
| 80 px = work.load() | |
| 81 parts = [] | |
| 82 for y in range(h): | |
| 83 for x in range(w): | |
| 84 if px[x, y] != 255: | |
| 85 continue | |
| 86 ImageDraw.floodfill(work, (x, y), 100) | |
| 87 part = work.point(lambda v: 255 if v == 100 else 0) | |
| 88 count = part.histogram()[255] | |
| 89 l, t, r, b = part.getbbox() | |
| 90 border = l == 0 or t == 0 or r == w or b == h | |
| 91 ImageDraw.floodfill(work, (x, y), 50) | |
| 92 px = work.load() | |
| 93 parts.append((count, (x, y), border)) | |
| 94 parts.sort(reverse=True) | |
| 95 return parts | |
| 96 | |
| 97 | |
| 98 def cut(sheet, box): | |
| 99 """cut one drawing out of the sheet. returns a trimmed RGBA image.""" | |
| 100 l, t, r, b = box | |
| 101 src = sheet.crop((max(0, l - PAD), max(0, t - PAD), | |
| 102 min(sheet.width, r + PAD), min(sheet.height, b + PAD))) | |
| 103 # pad with paper where the box reaches the sheet edge | |
| 104 im = Image.new("RGB", (r - l + 2 * PAD, b - t + 2 * PAD), (255, 255, 255)) | |
| 105 im.paste(src, (max(0, l - PAD) - (l - PAD), max(0, t - PAD) - (t - PAD))) | |
| 106 w, h = im.size | |
| 107 lum = im.convert("L") | |
| 108 | |
| 109 ink = lum.point(lambda v: 255 if v < INK_LUM else 0) | |
| 110 # close one pixel gaps in the outline | |
| 111 walls = ink.filter(ImageFilter.MaxFilter(3)) | |
| 112 | |
| 113 parts = outlines(walls) | |
| 114 largest = parts[0][0] | |
| 115 for i, (count, seed, border) in enumerate(parts): | |
| 116 if i == 0 or (count >= KEEP_SHARE * largest and not border): | |
| 117 ImageDraw.floodfill(walls, seed, 200) | |
| 118 kept = walls.point(lambda v: 255 if v == 200 else 0) | |
| 119 | |
| 120 # outside: flood fill from a corner, then grow back by the closed gap | |
| 121 ImageDraw.floodfill(kept, (0, 0), 128, thresh=0) | |
| 122 outside = kept.point(lambda v: 255 if v == 128 else 0) | |
| 123 drawing = outside.point(lambda v: 0 if v else 255) | |
| 124 outside = outside.filter(ImageFilter.MaxFilter(3)) | |
| 125 # only ink right next to the drawing counts as its edge | |
| 126 near = drawing.filter(ImageFilter.MaxFilter(5)) | |
| 127 | |
| 128 ramp = lum.point(lambda v: max(0, min(255, (LUM_CLEAR - v) * 255 // (LUM_CLEAR - INK_LUM)))) | |
| 129 alpha = Image.composite(ramp, Image.new("L", (w, h), 255), outside) | |
| 130 alpha = Image.composite(alpha, Image.new("L", (w, h), 0), near) | |
| 131 | |
| 132 rgb = im.point(lambda v: min(255, v * 255 // PAPER_LUM)) | |
| 133 out = rgb.convert("RGBA") | |
| 134 out.putalpha(alpha) | |
| 135 return out.crop(out.getbbox()) | |
| 136 | |
| 137 | |
| 138 def strip(levels): | |
| 139 """lay out level images side by side, centred vertically.""" | |
| 140 w = sum(im.width for im in levels) + STRIP_GAP * (len(levels) + 1) | |
| 141 h = max(im.height for im in levels) + 2 * STRIP_GAP | |
| 142 out = Image.new("RGBA", (w, h), (0, 0, 0, 0)) | |
| 143 x = STRIP_GAP | |
| 144 for im in levels: | |
| 145 out.paste(im, (x, (h - im.height) // 2)) | |
| 146 x += im.width + STRIP_GAP | |
| 147 return out | |
| 148 | |
| 149 | |
| 150 def main(path): | |
| 151 sheet = Image.open(path).convert("RGB") | |
| 152 for name, box in SPRITES.items(): | |
| 153 im = cut(sheet, box) | |
| 154 im.save(ART / f"{name}.png") | |
| 155 print(f"{name}: {im.width}x{im.height}") | |
| 156 for name, boxes in STRIPS.items(): | |
| 157 im = strip([cut(sheet, box) for box in boxes]) | |
| 158 im.save(ART / f"{name}.png") | |
| 159 print(f"{name}: {im.width}x{im.height}") | |
| 160 | |
| 161 | |
| 162 if __name__ == "__main__": | |
| 163 if len(sys.argv) != 2: | |
| 164 sys.exit("usage: cut_sheet.py SHEET") | |
| 165 main(sys.argv[1]) |
