-
Notifications
You must be signed in to change notification settings - Fork 0
/
glyphs.py
246 lines (239 loc) · 7.62 KB
/
glyphs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import numpy
def scale_array(arr, scale):
if (scale != int(scale)) or (scale < 1):
raise RuntimeError("scale={!r} must be a positive integer".format(scale))
elif scale == 1:
return arr
if len(arr.shape) == 2:
result = numpy.zeros(
(arr.shape[0] * scale, arr.shape[1] * scale),
dtype=arr.dtype)
for row in range(arr.shape[0]):
for col in range(arr.shape[1]):
for sub_row in range(scale):
for sub_col in range(scale):
result[row * scale + sub_row, col * scale + sub_col] = arr[row, col]
elif len(arr.shape) == 3:
result = numpy.zeros(
(arr.shape[0] * scale, arr.shape[1] * scale, arr.shape[2]),
dtype=arr.dtype)
for row in range(arr.shape[0]):
for col in range(arr.shape[1]):
for sub_row in range(scale):
for sub_col in range(scale):
for component in range(arr.shape[2]):
result[row * scale + sub_row,
col * scale + sub_col,
component] = arr[row, col, component]
else:
raise NotImplementedError("Only 2D greyscale or 2D color image "
"scaling is implemented")
return result
def colorize_greyscale(arr, color, bg_color=None):
basetype = numpy.uint8
try:
result = numpy.zeros((arr.shape[0], arr.shape[1], len(color)),
dtype=basetype)
except TypeError:
# color is a scalar (can't be enumerated)
if bg_color is None:
bg_color = 0
result = numpy.zeros((arr.shape[0], arr.shape[1]),
dtype=basetype)
for row in range(arr.shape[0]):
for col in range(arr.shape[1]):
result[row, col] = basetype(round(
arr[row, col] * (color - bg_color) + bg_color))
else:
# color can be enumerated
if bg_color is None:
bg_color = (0, 0, 0)
for row in range(arr.shape[0]):
for col in range(arr.shape[1]):
for (component, (value, bg_value)) in enumerate(zip(color, bg_color)):
result[row, col, component] = basetype(round(
arr[row, col] * (value - bg_value) + bg_value))
return result
def render_array_on_img(arr, img, x, y):
if len(arr.shape) != len(img.shape):
raise RuntimeError("arr and img must have same color type")
elif (len(arr.shape) == 3) and (arr.shape[2] != img.shape[2]):
raise RuntimeError("arr and img must have same color depth")
for row in range(arr.shape[0]):
for col in range(arr.shape[1]):
if (0 <= (col + x) < img.shape[1]) and (
0 <= (row + y) < img.shape[0]):
if len(arr.shape) == 3:
for component in range(len(arr.shape)):
img[row + y, col + x, component] = arr[row, col, component]
else:
img[row + y, col + x] = arr[row, col]
class Glyph(object):
def __init__(self, array):
self.data = {1: array,}
self.height = array.shape[0]
self.width = array.shape[1]
def scaled(self, scale):
try:
return self.data[scale]
except KeyError:
self.data[scale] = scale_array(self.data[1], scale)
return self.data[scale]
def render(self, img, x, y, scale=1, color=None, bg_color=None):
return render_array_on_img(
self.scaled(scale) if color is None
else colorize_greyscale(self.scaled(scale), color, bg_color),
img,
x,
y)
# 4 pixels below base line + 7 pixels for lower case + 3 pixels for ascenders
FONT = {
"a": Glyph(numpy.array((
(0, 0, 0, 0, 0, ),
(0, 0, 0, 0, 0, ),
(0, 0, 0, 0, 0, ),
(0, 1, 1, 1, 0, ),
(0, 0, 0, 0, 1, ),
(0, 0, 0, 0, 1, ),
(0, 1, 1, 1, 1, ),
(1, 0, 0, 0, 1, ),
(1, 0, 0, 0, 1, ),
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, 1, 1, 1, 1, ),
(1, 0, 0, 0, 0, ),
(1, 0, 0, 0, 0, ),
(0, 1, 1, 1, 1, ),
(0, 0, 0, 0, 0, ),
(0, 0, 0, 0, 0, ),
(0, 0, 0, 0, 0, ),
(0, 0, 0, 0, 0, ),))),
"i": Glyph(numpy.array((
(0, 0, 0, ),
(0, 1, 0, ),
(0, 0, 0, ),
(1, 1, 0, ),
(0, 1, 0, ),
(0, 1, 0, ),
(0, 1, 0, ),
(0, 1, 0, ),
(0, 1, 0, ),
(1, 1, 1, ),
(0, 0, 0, ),
(0, 0, 0, ),
(0, 0, 0, ),
(0, 0, 0, ),))),
"t": Glyph(numpy.array((
(0, 0, 0, ),
(0, 1, 0, ),
(1, 1, 1, ),
(0, 1, 0, ),
(0, 1, 0, ),
(0, 1, 0, ),
(0, 1, 0, ),
(0, 1, 0, ),
(0, 1, 0, ),
(0, 0, 1, ),
(0, 0, 0, ),
(0, 0, 0, ),
(0, 0, 0, ),
(0, 0, 0, ),))),
"l": Glyph(numpy.array((
(1, 1, 0, ),
(0, 1, 0, ),
(0, 1, 0, ),
(0, 1, 0, ),
(0, 1, 0, ),
(0, 1, 0, ),
(0, 1, 0, ),
(0, 1, 0, ),
(0, 1, 0, ),
(1, 1, 1, ),
(0, 0, 0, ),
(0, 0, 0, ),
(0, 0, 0, ),
(0, 0, 0, ),))),
" ": Glyph(numpy.array((
(0, 0, 0, 0, ),
(0, 0, 0, 0, ),
(0, 0, 0, 0, ),
(0, 0, 0, 0, ),
(0, 0, 0, 0, ),
(0, 0, 0, 0, ),
(0, 0, 0, 0, ),
(0, 0, 0, 0, ),
(0, 0, 0, 0, ),
(0, 0, 0, 0, ),
(0, 0, 0, 0, ),
(0, 0, 0, 0, ),
(0, 0, 0, 0, ),
(0, 0, 0, 0, ),))),
}
def render_string_on_img(str_, img, x, y, scale=1, color=None, font=FONT, line_spacing=1, glyph_spacing=1, bg_color=None):
start_x = x
max_h = 0
for c in str_:
if c == '\n':
y += max_h + line_spacing
x = start_x
else:
glyph = font[c]
glyph.render(img, x, y, scale, color, bg_color=bg_color)
max_h = max(max_h, glyph.height * scale)
x += glyph.width * scale + glyph_spacing
return img