|
1
|
1 """
|
|
|
2 image steps of the build: sprite trimming, tower level strips, map copies.
|
|
|
3
|
|
|
4 all functions take and return pillow images or file paths. no numpy; the
|
|
|
5 per-pixel loops run on crops of a few hundred pixels and stay fast enough.
|
|
|
6 """
|
|
|
7
|
|
|
8 from PIL import Image, ImageDraw, ImageFilter
|
|
|
9
|
|
|
10 # pencil lines in the tower strips are darker than this luminance
|
|
|
11 INK_LUM = 225
|
|
|
12 # light pixels outside a drawing fade from opaque at LUM_OPAQUE to
|
|
|
13 # transparent at LUM_CLEAR, which removes the white paper halo
|
|
|
14 LUM_OPAQUE = 200
|
|
|
15 LUM_CLEAR = 250
|
|
|
16 # gaps in the pencil outline up to about twice this many pixels are closed
|
|
|
17 # before the inside is filled
|
|
|
18 GAP_CLOSE = 2
|
|
|
19
|
|
|
20
|
|
|
21 def trim(im):
|
|
|
22 """return the image cropped to its non-transparent pixels."""
|
|
|
23 box = im.getbbox()
|
|
|
24 return im.crop(box) if box else im
|
|
|
25
|
|
|
26
|
|
|
27 def darken(im, factor):
|
|
|
28 """return a copy with the colour scaled by factor, alpha unchanged."""
|
|
|
29 r, g, b, a = im.split()
|
|
|
30 r, g, b = (c.point(lambda v: int(v * factor)) for c in (r, g, b))
|
|
|
31 return Image.merge("RGBA", (r, g, b, a))
|
|
|
32
|
|
|
33
|
|
6
|
34 def recolour(im, hue):
|
|
|
35 """
|
|
|
36 return a copy with every pixel's hue set to hue (degrees), saturation,
|
|
|
37 brightness and alpha unchanged. grey and black pixels stay as they are.
|
|
|
38 """
|
|
|
39 a = im.getchannel("A")
|
|
|
40 h, s, v = im.convert("RGB").convert("HSV").split()
|
|
|
41 h = h.point(lambda _: round(hue / 360 * 255))
|
|
|
42 out = Image.merge("HSV", (h, s, v)).convert("RGBA")
|
|
|
43 out.putalpha(a)
|
|
|
44 return out
|
|
|
45
|
|
|
46
|
|
1
|
47 def split_strip(im):
|
|
|
48 """
|
|
|
49 split a strip of drawings side by side at columns without pencil.
|
|
|
50
|
|
|
51 returns a list of crops, left to right. column runs narrower than
|
|
|
52 4 pixels count as specks and are dropped.
|
|
|
53 """
|
|
|
54 w, h = im.size
|
|
|
55 data = im.tobytes()
|
|
|
56 used = [False] * w
|
|
|
57 for y in range(h):
|
|
|
58 row = y * w * 4
|
|
|
59 for x in range(w):
|
|
|
60 i = row + x * 4
|
|
|
61 if data[i + 3] and data[i] + data[i + 1] + data[i + 2] < 3 * INK_LUM:
|
|
|
62 used[x] = True
|
|
|
63 runs = []
|
|
|
64 start = None
|
|
|
65 for x, u in enumerate(used + [False]):
|
|
|
66 if u and start is None:
|
|
|
67 start = x
|
|
|
68 elif not u and start is not None:
|
|
|
69 if x - start > 3:
|
|
|
70 runs.append((start, x))
|
|
|
71 start = None
|
|
|
72 # cut halfway between drawings so nothing of a drawing is lost
|
|
|
73 crops = []
|
|
|
74 for i, (a, b) in enumerate(runs):
|
|
|
75 left = 0 if i == 0 else (runs[i - 1][1] + a) // 2
|
|
|
76 right = w if i == len(runs) - 1 else (b + runs[i + 1][0]) // 2
|
|
|
77 crops.append(im.crop((left, 0, right, h)))
|
|
|
78 return crops
|
|
|
79
|
|
|
80
|
|
|
81 def fill_inside(im):
|
|
|
82 """
|
|
|
83 fill the inside of a pencil outline drawing with white.
|
|
|
84
|
|
|
85 the outside is found by a flood fill from the image border over pixels
|
|
|
86 that are not pencil. outline gaps are closed first by thickening the
|
|
|
87 pencil lines, and the outside is grown back by the same amount
|
|
|
88 afterwards. light pixels outside become transparent. returns a new
|
|
|
89 image, trimmed.
|
|
|
90 """
|
|
|
91 pad = GAP_CLOSE + 2
|
|
|
92 src = Image.new("RGBA", (im.width + 2 * pad, im.height + 2 * pad))
|
|
|
93 src.paste(im, (pad, pad))
|
|
|
94 w, h = src.size
|
|
|
95 data = src.tobytes()
|
|
|
96
|
|
|
97 ink = bytearray(w * h)
|
|
|
98 for i in range(w * h):
|
|
|
99 j = i * 4
|
|
|
100 if data[j + 3] > 64 and data[j] + data[j + 1] + data[j + 2] < 3 * INK_LUM:
|
|
|
101 ink[i] = 255
|
|
|
102 size = 2 * GAP_CLOSE + 1
|
|
|
103 walls = Image.frombytes("L", (w, h), bytes(ink)).filter(ImageFilter.MaxFilter(size))
|
|
|
104
|
|
|
105 # outside: flood fill from a corner, which the padding keeps free
|
|
|
106 ImageDraw.floodfill(walls, (0, 0), 128, thresh=0)
|
|
|
107 outside = walls.point(lambda v: 255 if v == 128 else 0)
|
|
|
108 outside = outside.filter(ImageFilter.MaxFilter(size)).tobytes()
|
|
|
109
|
|
|
110 out = bytearray(w * h * 4)
|
|
|
111 for i in range(w * h):
|
|
|
112 j = i * 4
|
|
|
113 r, g, b, a = data[j:j + 4]
|
|
|
114 lum = (r + g + b) / 3
|
|
|
115 if outside[i]:
|
|
|
116 # fade light pixels out, keep pencil
|
|
|
117 if lum >= LUM_CLEAR:
|
|
|
118 a = 0
|
|
|
119 elif lum > LUM_OPAQUE:
|
|
|
120 a = int(a * (LUM_CLEAR - lum) / (LUM_CLEAR - LUM_OPAQUE))
|
|
|
121 out[j:j + 4] = bytes((r, g, b, a))
|
|
|
122 else:
|
|
|
123 # composite over white
|
|
|
124 k = a / 255
|
|
|
125 out[j:j + 4] = bytes((int(r * k + 255 * (1 - k)),
|
|
|
126 int(g * k + 255 * (1 - k)),
|
|
|
127 int(b * k + 255 * (1 - k)), 255))
|
|
|
128 return trim(Image.frombytes("RGBA", (w, h), bytes(out)))
|
|
|
129
|
|
|
130
|
|
|
131 def tower_levels(path):
|
|
|
132 """return the four level images (levels 2-5) of a t*p strip."""
|
|
|
133 strip = Image.open(path).convert("RGBA")
|
|
|
134 levels = [fill_inside(c) for c in split_strip(strip)]
|
|
|
135 if len(levels) != 4:
|
|
|
136 raise ValueError(f"{path}: found {len(levels)} drawings, expected 4")
|
|
|
137 return levels
|
|
|
138
|
|
|
139
|
|
|
140 def map_copy(src, dst, quality):
|
|
|
141 """write the map scan as jpeg. maps are opaque, so nothing is lost."""
|
|
|
142 im = Image.open(src)
|
|
|
143 if im.mode == "RGBA" and im.getextrema()[3][0] < 255:
|
|
|
144 raise ValueError(f"{src}: map has transparent pixels, jpeg would lose them")
|
|
|
145 im.convert("RGB").save(dst, quality=quality, optimize=True)
|