-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoadTilesets.py
More file actions
434 lines (331 loc) · 11.6 KB
/
LoadTilesets.py
File metadata and controls
434 lines (331 loc) · 11.6 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
"""
"""
# Handle imports
from PIL import Image
import numpy as np
import os
import json
# Define constants
tileset_info_file = os.path.join("resources", "tileset_info.txt")
precompute_tileset_info_files = [
os.path.join("resources", "tmp_tileset_info.txt"),
os.path.join("resources", "default_tileset_info.txt")
]
tileset_info = None
max_checks = 4 # Max number of color guesses
min_tile_length = 2 # Anything smaller causes subtle bugs in code >.>
max_tile_length = 32 # They're slooowwww
epsilon = 0.001
# Define methods
def load_tileset_info():
"""
Loads tileset info files and associated images.
Does some precomputation.
:param files: Tileset info files
:return: All the info merged into a list.
"""
global tileset_info_file, precompute_tileset_info_files, min_tile_length, max_tile_length
print("Loading in tileset info.")
recompute_part1 = False
if not os.path.exists(tileset_info_file):
# The tileset info file doesn't exist.
recompute_part1 = True
else:
for file in precompute_tileset_info_files:
if os.path.getmtime(file) > os.path.getmtime(tileset_info_file):
# A file was updated.
recompute_part1 = True
if recompute_part1:
precompute_tilesets_part1(precompute_tileset_info_files, tileset_info_file)
precompute_tilesets_part2(min_tile_length, max_tile_length, tileset_info_file)
def precompute_tilesets_part1(precompute_tileset_info_files, tileset_info_file):
"""
Compute non-numpy, easily chacheable data about the tilesets.
:param precompute_tileset_info_files: Tileset info files from TilesetScrapper.py
:param tileset_info_file: Where to output computed info to.
:return: Nothing, really. *sweats*
"""
to_return = []
for file in precompute_tileset_info_files:
with open(file, "r") as f:
to_return.extend(json.loads(f.read()))
for info in to_return:
local_filepath = info["local_filepath"]
image = image_to_array(local_filepath)
use_alpha = check_image_alpha(local_filepath)
tile_shape = [image.shape[0] // 16, image.shape[1] // 16]
combined_color_guesses = []
for y in range(16):
for x in range(16):
tile = image[
y * tile_shape[0]: (y + 1) * tile_shape[0],
x * tile_shape[1]: (x + 1) * tile_shape[1]
]
combined_color_guesses.append(
tile_color_guesses(tile, use_alpha))
info["alpha"] = use_alpha
info["shape"] = tile_shape
info["size"] = tile_shape[0] * tile_shape[1]
info["color_guesses"] = combined_color_guesses
with open(tileset_info_file, "w+") as f:
f.write(json.dumps(to_return, indent=2))
print("Finished precomputing part 1.")
def precompute_tilesets_part2(min_tile_length, max_tile_length, tileset_info_file):
"""
Compute more information about the tilesets.
:return: Nothing.
"""
global tileset_info
to_return = []
with open(tileset_info_file, "r") as f:
to_return.extend(json.loads(f.read()))
for i in range(len(to_return)-1, -1, -1):
info = to_return[i]
image = image_to_array(info["local_filepath"])
tiles = []
tile_shape = info["shape"]
if tile_shape[0] < min_tile_length or tile_shape[1] < min_tile_length:
del to_return[i]
continue
if tile_shape[0] > max_tile_length or tile_shape[1] > max_tile_length:
del to_return[i]
continue
for y in range(16):
for x in range(16):
tile = image[
y * tile_shape[0]: (y + 1) * tile_shape[0],
x * tile_shape[1]: (x + 1) * tile_shape[1]
]
tile_info = {}
tile_info["image"] = tile
tile_info["color_guesses"] = info["color_guesses"][x + y*16]
tile_info["pink_mask"] = tile_pink_mask(tile)
tiles.append(tile_info)
info["tiles"] = np.array(tiles)
info["hashes"] = calculate_tileset_info(info["tiles"], info["alpha"])
tileset_info = to_return
print("Finished precomputing part 2.")
def calculate_tileset_info(tiles, use_alpha):
"""
Calculate additional info for an entire tileset.
:param tiles: Tileset tiles
:param use_alpha: If the tileset uses alpha
:return: Additional tileset info
"""
hashes = []
for i in range(len(tiles)):
tile = tiles[i]
hashes.append(hash_tile(tile["image"]))
return np.array(hashes)
def tile_pink_mask(tile):
"""
Precompute which parts of a pink tile are background.
:param tile: A tileset tile
:return: The tile's pink mask
"""
# A heuristic
is_pink = (tile[:, :, 0:1] > 250) * \
(tile[:, :, 1:2] < 5) * \
(tile[:, :, 2:3] > 250)
return is_pink.astype(int)
def hash_tile(tile):
"""
Quickly calculate a vector of a tile that is color invariant and noise resistant.
:param tile: A tileset tile
:return: A numpy vector
"""
global epsilon
# Make Hue invariant
tile = np.sum(tile, axis=2)
# Make 1D
tile.resize([tile.shape[0] * tile.shape[1]])
# Calculate diff between consecutive pixels
diff = np.convolve(tile, np.array([1, -1]), 'valid')
np.abs(diff, out=diff)
diff -= diff.min()
return diff / (np.linalg.norm(diff) + epsilon)
def entropy_image(image):
"""
Calculates the entropy of types of colors in an image.
:param image:
:return:
"""
prob = np.sort(
np.unique(np.resize(image, [-1, image.shape[2]]), return_counts=True,
axis=0)[1]) / (
image.shape[0] * image.shape[1])
return np.sum(prob * np.log(prob))
def tile_color_guesses(tile, use_alpha):
"""
Precompute pixels to check for obtaining foreground and background colors of a tile.
:param tile:
:param use_alpha:
:return:
"""
global max_checks
# We have to track foreground and background colors for checking purposes
tile_shape = tile.shape
checks = []
foreground_c = False
background_c = False
reset = False
done = False
# Check each and every pixel
x = 0
while x < tile_shape[1] and not done:
y = 0
while y < tile_shape[0] and not done:
tileset_c = tile[y, x]
alpha = tileset_c[3] / 255
tileset_c = tileset_c[:3]
is_pink = tileset_c[0] > 250 and tileset_c[1] < 5 \
and tileset_c[2] > 250
if is_pink and (not use_alpha):
if not background_c:
# The background color and sample color should be the same
# Limit to one check, because bleh otherwise
checks.append({"type": "p_b", "pos": (y, x)})
reset = True # Reset scan with new information
background_c = True
else:
average = np.max(
tileset_c) # Aka value (HSV) // 255 = fore, 0 = back
transparency = average / 255
"""
We need to guess the foreground color.
If the background color is known, this is easy.
If alpha = 1.0, we can ignore the background color.
"""
if alpha == 1 or (
background_c is True and alpha != 0.0):
if len(checks) < max_checks - 1 or background_c:
checks.append({"type": "f", "pos": (y, x)})
if not foreground_c:
reset = True # Reset scan with new information
foreground_c = True
"""
We need to guess the background color.
It's pretty much the same as guessing the foreground color.
"""
if (alpha == 0) or (
foreground_c is True and alpha != 1) or (
transparency == 0):
if len(checks) < max_checks - 1 or foreground_c:
checks.append({"type": "b", "pos": (y, x)})
if not background_c:
reset = True # Reset scan with new information
background_c = True
if (foreground_c and background_c) and len(checks) == max_checks:
done = True
y += 1
if reset:
y = tile_shape[0]
x += 1
if reset:
x, y = 0, 0
reset = False
return checks
def check_image_alpha(image_path):
"""
Check if an image uses alpha.
:param image_path:
:return:
"""
with Image.open(image_path) as image:
im_arr = np.fromstring(image.tobytes(), dtype=np.uint8)
im_arr = im_arr.reshape((image.size[0], image.size[1], -1)).astype(int)
return im_arr.shape[2] == 4
def image_to_array(image_path):
"""
Loads image into 3D Numpy array of shape
(width, height, 4)
Where 4 represents RGBA
:param image_path: The location of the image
:return: 3d numpy array
"""
with Image.open(image_path) as image:
image = image.convert('RGBA')
im_arr = np.fromstring(image.tobytes(), dtype=np.uint8)
im_arr = im_arr.reshape((image.size[1], image.size[0], -1))
return im_arr
def get_tileset(name):
"""
Get info for a given tileset.
:param name: Tileset name
:return: Tileset info
"""
global tileset_info
for info in tileset_info:
if name.lower() in info["local_filename"].lower():
return info
else:
return None
def get_tileset_by_id(i):
"""
Get a tileset by an id.
:param i: An id.
:return: A corresponding tileset.
"""
global tileset_info
return tileset_info[i]
def get_id_of_tileset(name):
"""
Get an id of a tileset. Returns -1 if nothing matches.
:param i: An id.
:return: A corresponding tileset.
"""
global tileset_info
id = 0
for info in tileset_info:
if name.lower() in info["local_filename"].lower():
return id
id += 1
else:
return -1
def num_tilesets():
"""
Get the number of tilesets.
:return: The number of tilesets
"""
global tileset_info
return len(tileset_info)
def largest_tile_dims(tileset_ids = None):
"""
Get the largest tileset tile dimension.
:return: A tuple.
"""
global tileset_info
max_x = 0
max_y = 0
if tileset_ids is None:
tileset_ids = list(range(num_tilesets()))
for tileset_id in tileset_ids:
info = tileset_info[tileset_id]
if info["shape"][0] > max_y:
max_y = info["shape"][0]
if info["shape"][1] > max_x:
max_x = info["shape"][1]
largest_dims = [max_y, max_x]
return largest_dims
smallest_dims = None
def smallest_tile_dims(tileset_ids = None):
"""
Get the largest tileset tile dimension.
:return: A tuple.
"""
global tileset_info
min_x = 1000
min_y = 1000
if tileset_ids is None:
tileset_ids = list(range(num_tilesets()))
for tileset_id in tileset_ids:
info = tileset_info[tileset_id]
if info["shape"][0] < min_y:
min_y = info["shape"][0]
if info["shape"][1] < min_x:
min_x = info["shape"][1]
smallest_dims = [min_y, min_x]
return smallest_dims
# Temp code
# tileset = image_to_array("resources/test/Curses 640x300diag.png")
# np.save('resources/test/Curses', tileset)