-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHelperTools.py
159 lines (137 loc) · 5.31 KB
/
HelperTools.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
"""Helper functions for the tileset generation process"""
import shutil
from os import listdir, makedirs
from os.path import join, exists
from multiprocessing import Pool
from sys import argv
# https://docs.wand-py.org/
from wand.image import Image
def make_dir(path):
"""custom mkdir"""
if not exists(path):
makedirs(path)
def ImageToTileset(source, dest):
# see if directory already exists
if not exists(source):
print("Source image does not exist")
return
make_dir(dest)
print("Exporting multiple sizes of your source image...")
maps_path = join(dest, "maps")
make_dir(maps_path)
ImageToSizes(source, dest)
sizes = sorted(listdir(maps_path))
grid_path = join(dest, "grid")
make_dir(grid_path)
print("Converting each size into a Tileset...")
for i in sizes:
size = len(sizes) - int(i[: i.rfind(".")])
print("Creating grid for zoom level " + str(size))
source_path = join(maps_path, i)
# this is jank but we need to get width to GridToTileset
path_to_grid_done = join(dest, str(size))
if exists(path_to_grid_done) and len(listdir(path_to_grid_done)) > 0:
print("Grid already done")
continue
width = ImageToGrid(source_path, grid_path)
folder_path = join(dest, str(size))
make_dir(folder_path)
print("Arranging grid into directory")
dest_path_sized = join(dest, str(size))
GridToTileset(grid_path, dest_path_sized, int(width / 256))
print("Done!")
# explort multiple sizes of a source image
def ImageToSizes(source, dest):
print(f"Exporting {source}")
if len(listdir(join(dest, "maps"))) > 0:
print("Already exported")
return
with Image(filename=source) as img:
dim = (img.size[0] * 2, img.size[1] * 2)
ind = 0
while dim[0] % 256 == 0 and dim[1] % 256 == 0:
output_path = join(dest, "maps", str(ind) + ".png")
if exists(output_path):
print(f"Skipping {output_path}")
ind = ind + 1
dim = (int(dim[0] / 2), int(dim[1] / 2))
continue
print("Exporting size " + str(ind))
# export each smaller version
with img.clone() as i:
i.resize(dim[0], dim[1], filter="point") # nearest neighbor filter
i.save(filename=output_path)
dim = (int(dim[0] / 2), int(dim[1] / 2))
ind = ind + 1
# split image into 256x256 sub-images
def ImageToGrid(source, dest):
with Image(filename=source) as img:
i = 0
for h in range(0, img.size[1], 256):
for w in range(0, img.size[0], 256):
output_path = join(dest, f"{i:06d}.png")
if exists(output_path):
print(f"Skipping {output_path}")
i = i + 1
continue
w_end = w + 256
h_end = h + 256
with img[w:w_end, h:h_end] as tile:
tile.save(filename=output_path)
i = i + 1
return img.size[0]
# arrange a folder of grid images into a tileset
def GridToTileset(source, dest, width):
files = sorted(listdir(source))
ptr = 0
ptr2 = -1
for f in files:
if ptr == 0:
ptr2 = ptr2 + 1
make_dir(join(dest, str(ptr2)))
shutil.move(join(source, f), join(dest, str(ptr2), str(ptr) + ".png"))
ptr = ptr + 1
ptr = ptr % width
def Translate(x, y, w, h):
x1 = (x / 16) - 1
y1 = -((y / 16) - 1)
w = w / 16
h = h / 16
x2 = x1 + w
y2 = y1 - h
return f"[[getCordfromLoc({y1}, {x1}), getCordfromLoc({y2}, {x1}), getCordfromLoc({y2}, {x2}), getCordfromLoc({y1}, {x2}), getCordfromLoc({y1}, {x1})]]"
# [[getCordfromLoc(-234.0625, 207.5), getCordfromLoc(-262.5625, 207.5), getCordfromLoc(-262.5625, 239.5), getCordfromLoc(-234.0625, 239.5), getCordfromLoc(-234.0625, 207.5)]]
def TranslateScale(s, x, y, w, h):
x1 = (x / s) - 1
y1 = -((y / s) - 1)
w = w / s
h = h / s
x2 = x1 + w
y2 = y1 - h
return f"[[getCordfromLoc({y1}, {x1}), getCordfromLoc({y2}, {x1}), getCordfromLoc({y2}, {x2}), getCordfromLoc({y1}, {x2}), getCordfromLoc({y1}, {x1})]]"
# [[getCordfromLoc(-234.0625, 207.5), getCordfromLoc(-262.5625, 207.5), getCordfromLoc(-262.5625, 239.5), getCordfromLoc(-234.0625, 239.5), getCordfromLoc(-234.0625, 207.5)]]
def wrapper_to_tileset(args_of_func):
"""wrapper to call ImageToTileset with a single argument"""
return ImageToTileset(args_of_func[0], args_of_func[1])
# https://golb.n4n5.dev/python
def multi_process(my_func, my_args, num_processes=10):
"""my_args is a table of arguments for my_func"""
results = []
with Pool(processes=num_processes) as pool:
for result in pool.imap(my_func, my_args):
results.append(result)
return results
if __name__ == "__main__":
args = [
[
join("maps", one_map),
join("public", "tilesets", one_map[: one_map.rfind(".")].lower()),
]
for one_map in sorted(listdir("maps"))
]
if len(argv) > 1:
multi_process(wrapper_to_tileset, args, num_processes=int(argv[1]))
else:
# single process - run in current process
for arg in args:
wrapper_to_tileset(arg)