forked from amnh-sciviz/collectionscope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare_textures.py
166 lines (140 loc) · 5.99 KB
/
prepare_textures.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
# -*- coding: utf-8 -*-
import argparse
import math
import numpy as np
import os
from PIL import Image
from pprint import pprint
import sys
import lib.image_utils as iu
import lib.io_utils as io
import lib.item_utils as tu
import lib.list_utils as lu
import lib.math_utils as mu
# input
parser = argparse.ArgumentParser()
parser.add_argument("-config", dest="CONFIG_FILE", default="config-sample.yml", help="Config file")
parser.add_argument("-cache", dest="CACHE_DIR", default="tmp/", help="Directory for caching image data")
parser.add_argument('-probe', dest="PROBE", action="store_true", help="Just output details?")
a = parser.parse_args()
config = tu.loadConfig(a.CONFIG_FILE)
maxWidth = 4096
maxTextureFiles = 10
minCellWidth = 1
maxCellWidth = 512
containsAlpha = config["imageHasAlpha"] if "imageHasAlpha" in config else False
defaultColor = config["defaultColor"] if "defaultColor" in config else "#3C3C3C"
filenameKey = config["filenameColumn"] if "filenameColumn" in config else "filename"
imageDir = config["imageDirectory"]
noImageValue = config["noImageValue"] if "noImageValue" in config else None
OUTPUT_DIR = "apps/{appname}/".format(appname=config["name"])
OUTPUT_TEXTURES_DIR_REL = "img/textures/"
OUTPUT_TEXTURES_DIR = OUTPUT_DIR + OUTPUT_TEXTURES_DIR_REL
CONFIG_FILE = OUTPUT_DIR + "js/config/config.textures.js"
if not a.PROBE:
# Make sure output dirs exist
io.makeDirectories([OUTPUT_TEXTURES_DIR, CONFIG_FILE, a.CACHE_DIR])
# Remove existing images
io.removeFiles(OUTPUT_TEXTURES_DIR + "*.jpg")
items, categories = tu.getItems(config)
# Make texture for each set
# sets = list(configSets.items())
sets = [] # just produce the default set for now
sets = [("default", {"query": ""})] + sets # add default set
jsonsets = {}
for keyName, options in sets:
setItems = lu.filterByQueryString(items, options["query"])
if len(setItems) > 0:
print("%s results found for '%s'" % (len(setItems), options["query"]))
else:
print("Warning: '%s' produces no results" % options["query"])
continue
# limit the results if specified
if "limit" in options and len(setItems) > options["limit"]:
setItems = setItems[:options["limit"]]
setCount = len(setItems)
# determine ideal cell width
cellWidth = maxCellWidth
textureFileCount = 1
cellsPerFile = 1
while True:
cellsPerFile = (maxWidth / cellWidth) ** 2
textureFileCount = math.ceil(1.0 * setCount / cellsPerFile)
if textureFileCount <= maxTextureFiles:
print(" -> Found ideal cell width for %s: %spx with file count %s" % (keyName, cellWidth, textureFileCount))
break
if cellWidth <= minCellWidth:
print(" -> Too many items! Minimum file count will be %s when cell width is %s (the minumum cell width)" % (textureFileCount, minCellWidth))
sys.exit()
break
cellWidth /= 2
if a.PROBE:
continue
cellsPerFile = int(cellsPerFile)
cellWidth = int(cellWidth)
textureFileCount = int(textureFileCount)
cacheFilename = a.CACHE_DIR + "%s_%s_%s.p.gz" % (config["name"], keyName, cellWidth)
imageData = io.readCacheFile(cacheFilename)
colors = 3 if containsAlpha < 1 else 4
colorMode = "RGB" if colors == 3 else "RGBA"
# if no cache, read and process images and save cache
if imageData is None:
print("No cache for %s.. reading image data..." % keyName)
shape = (setCount, cellWidth, cellWidth, colors)
imageData = np.zeros(shape, dtype=np.uint8)
for i, item in enumerate(setItems):
basename = item[filenameKey]
fn = imageDir + basename
if "{" in fn:
fn = fn.format(**item)
# check for no image
if noImageValue is not None and basename == noImageValue or not os.path.isfile(fn):
print("No image value for %s" % fn)
img = Image.new(mode=colorMode, size=(cellWidth, cellWidth), color=defaultColor)
else:
img = iu.readImage(fn, mode=colorMode)
if img is not None:
img = iu.containImage(img, cellWidth, cellWidth)
if img is None:
print("Error loading %s" % fn)
img = Image.new(mode=colorMode, size=(cellWidth, cellWidth), color=defaultColor)
imageData[i] = np.array(img)
mu.printProgress(i+1, setCount)
io.writeCacheFile(cacheFilename, imageData)
setCount, cellWidth, cellWidth, colors = imageData.shape
# build texture images
startIndex = 0
format = "jpg" if colors == 3 else "png"
bgColor = [0, 0, 0] if colors == 3 else [0, 0, 0, 0]
cellsPerRow = int(maxWidth / cellWidth)
assetFiles = []
for fileIndex in range(textureFileCount):
textureImageFn = OUTPUT_TEXTURES_DIR + "%s_%s.%s" % (keyName, fileIndex, format)
textureImageFnRel = OUTPUT_TEXTURES_DIR_REL + "%s_%s.%s" % (keyName, fileIndex, format)
endIndex = startIndex + cellsPerFile
fileImageData = imageData[startIndex:] if endIndex >= setCount else imageData[startIndex:endIndex]
baseImg = Image.new(mode=colorMode, size=(maxWidth, maxWidth), color=tuple(bgColor))
basePixels = np.zeros((maxWidth, maxWidth, colors), dtype=np.uint8)
for i in range(len(fileImageData)):
col = int(i % cellsPerRow)
row = int(i / cellsPerRow)
x = col * cellWidth
y = row * cellWidth
basePixels[y:y+cellWidth, x:x+cellWidth] = fileImageData[i]
iu.savePixelsToImage(textureImageFn, basePixels)
startIndex += cellsPerFile
assetFiles.append({"src": textureImageFnRel})
jsonsets[keyName] = {
"width": maxWidth,
"height": maxWidth,
"cellWidth": cellWidth,
"cellHeight": cellWidth,
"assets": assetFiles
}
if a.PROBE:
sys.exit()
# Write config file
outjson = {
"textures": jsonsets
}
io.writeJSON(CONFIG_FILE, outjson, pretty=True, prepend="_.extend(CONFIG, ", append=");")