forked from OldGamesLab/Harold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exportImagesPar.py
153 lines (113 loc) · 4.61 KB
/
exportImagesPar.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
"""
Copyright 2014-2015 darkf
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
# Converts data/art/*/*.FRM and *.FR[0-5] images to .png images
import sys, os, glob, json, time, multiprocessing
import pal
import frmpixels
N_PROCS = 2
CHUNKSIZE = 4
SUBDIRS = ("inven", "tiles", "critters", "items", "scenery", "walls", "misc", "intrface") # etc
def convertFRM(task):
(name, FRM, outpath, palette, exportImage) = task
return ('art/'+name, frmpixels.exportFRM(FRM, outpath, palette, exportImage))
def convertFRX(task):
(name, images, outpath, palette, exportImage) = task
return ('art/'+name, frmpixels.exportFRMs(images, outpath, palette, exportImage))
def getFRMTasks(palette, dataDir, outDir, exportImage=True):
subdirFRMs = [glob.glob("%s/art/%s/*.frm" % (dataDir, subdir)) for subdir in SUBDIRS]
#totalNum = sum(len(x) for x in subdirFRMs)
for subdirIdx,FRMs in enumerate(subdirFRMs):
subdir = SUBDIRS[subdirIdx]
for FRM in FRMs:
name = '%s/%s' % (subdir, os.path.splitext(os.path.basename(FRM))[0].lower())
outpath = "%s/%s.png" % (outDir, name)
yield (name, FRM, outpath, palette, exportImage)
def getFRXTasks(palette, dataDir, outDir, exportImage=True):
subdirFRMs = [glob.glob("%s/art/%s/*.fr0" % (dataDir, subdir)) for subdir in SUBDIRS]
#totalNum = sum(len(x) for x in subdirFRMs)
for subdirIdx,FRMs in enumerate(subdirFRMs):
subdir = SUBDIRS[subdirIdx]
for FRM in FRMs:
basename = os.path.splitext(os.path.basename(FRM))[0].lower()
images = glob.glob("%s/art/%s/%s.fr[0-5]" % (dataDir, subdir, basename))
#print "images:", images
# TODO: validate that images are ordered FR0 to FRn
name = '%s/%s' % (subdir, basename)
outpath = "%s/%s.png" % (outDir, name)
yield (name, images, outpath, palette, exportImage)
def flatten(l):
return [item for sublist in l for item in sublist]
def readPAL(path):
palette = pal.readPAL(open(path, "rb"))
palette = flatten([r, g, b] for r, g, b in palette)
return palette
def convertAll(palette, dataDir, outDir, mode='both', imageMapMode='yes', nProcs=N_PROCS, verbose=False):
# Convert FRMs and FR[0-9]s, and output an image map
start_time = time.perf_counter()
if not os.path.exists(outDir):
os.mkdir(outDir)
exportImage = imageMapMode != 'only'
imageInfo = {}
for subdir in SUBDIRS:
dir = os.path.join(outDir, subdir)
if not os.path.exists(dir):
os.mkdir(dir)
pool = multiprocessing.Pool(processes=nProcs)
if mode == 'frx' or mode == 'both':
if verbose: print("scanning directories for FR[0-5]s...")
tasks = list(getFRXTasks(palette, dataDir, outDir, exportImage))
numTasks = len(tasks)
i = 1
if verbose: print(f"processing {numTasks} FR[0-5]s...")
for (k,v) in pool.imap(convertFRX, tasks, chunksize=CHUNKSIZE):
if verbose: print(f"[{i}/{numTasks}] {k}...")
imageInfo[k] = v
i += 1
if mode == 'frm' or mode == 'both':
if verbose:
print("")
print("scanning directories for FRMs...")
tasks = list(getFRMTasks(palette, dataDir, outDir, exportImage))
numTasks = len(tasks)
i = 1
if verbose: print("processing FRMs...")
for (k,v) in pool.imap(convertFRM, tasks, chunksize=CHUNKSIZE):
if verbose: print(f"[{i}/{numTasks}] {k}...")
imageInfo[k] = v
i += 1
if imageMapMode != 'no':
if verbose: print("writing image map...")
# write new imageMap
json.dump(imageInfo, open(outDir + "/imageMap.json", "w"))
return time.perf_counter() - start_time
def main():
if len(sys.argv) < 5:
print(f"USAGE: {sys.argv[0]} PALETTE DATA_DIR OUT_DIR MODE [--no-map] [--only-map]")
print("MODE is either 'frm' for .FRMs only, 'frx' for .FR[0-5]s only, or 'both' for both.")
print("PALETTE is likely data/color.pal, and DATA_DIR is likely data/")
print("OUT_DIR is wherever you want the exported images and map to go")
sys.exit(1)
palettePath = sys.argv[1]
dataDir = sys.argv[2]
outDir = sys.argv[3]
mode = sys.argv[4]
palette = readPAL(palettePath)
imageMapMode = 'yes'
if '--no-map' in sys.argv:
imageMapMode = 'no'
if '--only-map' in sys.argv:
imageMapMode = 'only'
elapsedTime = convertAll(palette, dataDir, outDir, mode, imageMapMode, verbose=True)
print(f"Took {elapsedTime} seconds")
if __name__ == '__main__':
main()