-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexport_result.py
More file actions
277 lines (218 loc) · 10.7 KB
/
export_result.py
File metadata and controls
277 lines (218 loc) · 10.7 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
import abc
import base64
import io
import json
import logging
import os.path
import sys
import xml.etree.cElementTree as ET
import PIL.Image
import numpy as np
import svgwrite
import tqdm
from PIL import Image
import argparse
import analysis
import hierarchy_node
import common
EXTENSION_INFERENCE = {
"xml": "xml",
"json": "json",
"svg": "svg",
"png": "raster",
"jpg": "raster",
"jpeg": "raster",
"bmp": "raster",
"gif": "raster",
"webp": "raster",
"pdf": "raster",
}
class ResultExporter(abc.ABC):
def __call__(self, output_path: str, result_path: str):
logging.info("Reading save files at '{}'...".format(os.path.abspath(output_path)))
primary_textons = hierarchy_node.VectorNode.load(os.path.join(output_path, "primary_textons.dat"))
secondary_textons = hierarchy_node.VectorNode.load(os.path.join(output_path, "secondary_textons.dat"))
gradient_field = analysis.result_objects.GradientFieldResult.load(os.path.join(output_path, "gradient_field.dat"))
if isinstance(primary_textons, analysis.result_objects.PrimaryTextonResult):
primary_textons = primary_textons.primary_textons
if isinstance(secondary_textons, analysis.result_objects.SecondaryTextonResult):
secondary_textons = secondary_textons.secondary_textons
gradient_field_raster = np.asarray(PIL.Image.open(os.path.join(output_path, "gradient_field.png"))) / 255
logging.info("Converting files...")
self._convert_result(primary_textons, secondary_textons, gradient_field, gradient_field_raster, result_path)
@abc.abstractmethod
def _convert_result(
self, primary_textons: hierarchy_node.VectorNode, secondary_textons: hierarchy_node.VectorNode,
gradient_field: analysis.result_objects.GradientFieldResult, gradient_field_raster: np.ndarray,
result_path: str
):
pass
@staticmethod
def _parent_texton_to_dict(texton: hierarchy_node.VectorNode, string_coordinates: bool) -> dict:
exterior = " ".join("{},{}".format(*coord) for coord in texton.exterior) if string_coordinates else texton.exterior.astype(float).tolist()
centroid = "{},{}".format(*texton.get_centroid()) if string_coordinates else texton.get_centroid().astype(float).tolist()
return dict(
exterior=exterior,
category=str(texton.category) if string_coordinates else int(texton.category),
color='#%02x%02x%02x' % tuple(texton.color_to_int()),
area=str(texton.get_area()) if string_coordinates else texton.get_area(),
centroid=centroid
)
@staticmethod
def _child_texton_to_dict(texton: hierarchy_node.VectorNode, string_coordinates: bool) -> dict:
exterior = " ".join("{},{}".format(*coord) for coord in texton.exterior) if string_coordinates else texton.exterior.astype(float).tolist()
centroid = "{},{}".format(*texton.get_centroid()) if string_coordinates else texton.get_centroid().astype(float).tolist()
return dict(
exterior=exterior,
color='#%02x%02x%02x' % tuple(texton.color_to_int()),
color_delta=None if texton.color_delta is None else '#%02x%02x%02x' % tuple(texton.color_delta_to_int()),
area=str(texton.get_area()) if string_coordinates else texton.get_area(),
centroid=centroid
)
def get_name(self) -> str:
return self.__class__.__name__
@abc.abstractmethod
def get_default_extension(self) -> str:
pass
class XMLExporter(ResultExporter):
def _convert_result(
self, primary_textons: hierarchy_node.VectorNode, secondary_textons: hierarchy_node.VectorNode,
gradient_field: analysis.result_objects.GradientFieldResult, gradient_field_raster: np.ndarray,
result_path: str
):
logging.info("Creating root element...")
root = ET.Element(
"result",
width=str(primary_textons.get_bounding_width(as_int=True)),
height=str(primary_textons.get_bounding_height(as_int=True)),
version="1.0.0"
)
primary = ET.SubElement(root, "primary", polygon_count=str(len(primary_textons.children)))
primary_textons.to_xml(primary)
secondary = ET.SubElement(root, "secondary", polygon_count=str(len(secondary_textons.children)))
secondary_textons.to_xml(secondary)
gradient = ET.SubElement(
root, "gradient", point_count=str(len(gradient_field.points)), point_spacing=str(gradient_field.query_point_spacing)
)
for point, color in zip(gradient_field.points, gradient_field.colors):
ET.SubElement(
gradient, "point",
color='#%02x%02x%02x' % tuple((np.clip(color, 0, 1) * 255).astype(int)),
point="{},{}".format(*point)
)
logging.info("Saving to '{}'...".format(os.path.abspath(result_path)))
tree = ET.ElementTree(root)
tree.write(result_path, xml_declaration=True, encoding="utf-8")
def get_default_extension(self) -> str:
return ".xml"
class JSONExporter(ResultExporter):
def _convert_result(
self, primary_textons: hierarchy_node.VectorNode, secondary_textons: hierarchy_node.VectorNode,
gradient_field: analysis.result_objects.GradientFieldResult, gradient_field_raster: np.ndarray,
result_path: str
):
result = dict(
width=primary_textons.get_bounding_width(as_int=True),
height=primary_textons.get_bounding_height(as_int=True),
version="1.0.0",
primary=dict(polygon_count=len(primary_textons.children), textons=primary_textons.to_json(include_self=False)),
secondary=dict(polygon_count=len(secondary_textons.children), textons=secondary_textons.to_json(include_self=False)),
gradient=dict(total=len(gradient_field.points), point_spacing=gradient_field.query_point_spacing, points=[]),
)
for point, color in zip(gradient_field.points, gradient_field.colors):
result["gradient"]["points"].append(dict(
color='#%02x%02x%02x' % tuple((np.clip(color, 0, 1) * 255).astype(int)),
point=point.astype(float).tolist()
))
with open(result_path, 'w') as f:
f.write(json.dumps(result, separators=(',', ':')))
def get_default_extension(self) -> str:
return ".json"
class SVGExporter(ResultExporter):
def _convert_result(
self, primary_textons: hierarchy_node.VectorNode, secondary_textons: hierarchy_node.VectorNode,
gradient_field: analysis.result_objects.GradientFieldResult, gradient_field_raster: np.ndarray,
result_path: str
):
size = (primary_textons.get_bounding_width(), primary_textons.get_bounding_height())
dwg = svgwrite.Drawing(result_path, profile='tiny', size=size)
logging.info("Converting gradient velocity_field...")
image_pil = Image.fromarray((gradient_field_raster * 255).astype(np.uint8))
png_buffer = io.BytesIO()
image_pil.save(png_buffer, format='PNG')
png_data = png_buffer.getvalue()
base64_png = base64.b64encode(png_data).decode('ascii')
data_uri = f"data:image/png;base64,{base64_png}"
dwg.add(dwg.image(href=data_uri, insert=(0, 0), size=size))
logging.info("Converting secondary textons...")
secondary_textons.to_svg(dwg, include_self=False)
logging.info("Converting primary textons...")
primary_textons.to_svg(dwg, include_self=False)
logging.info("Saving to '{}'...".format(os.path.abspath(result_path)))
dwg.save()
def get_default_extension(self) -> str:
return ".svg"
class RasterExporter(ResultExporter):
def _convert_result(
self, primary_textons: hierarchy_node.VectorNode, secondary_textons: hierarchy_node.VectorNode,
gradient_field: analysis.result_objects.GradientFieldResult, gradient_field_raster: np.ndarray,
result_path: str
):
logging.info("Converting gradient velocity_field...")
background = Image.fromarray((gradient_field_raster * 255).astype(np.uint8))
logging.info("Converting secondary textons...")
secondary = secondary_textons.to_pil(background_color=(0, 0, 0, 0), rgba=True)
background.paste(secondary, (0, 0), secondary)
logging.info("Converting primary textons...")
primary = primary_textons.to_pil(background_color=(0, 0, 0, 0), rgba=True)
background.paste(primary, (0, 0), primary)
logging.info("Saving to '{}'...".format(os.path.abspath(result_path)))
background.save(result_path)
def get_default_extension(self) -> str:
return ".png"
if __name__ == '__main__':
common.logger.configure_logger()
parser = argparse.ArgumentParser(
description="Exports a synthesized texture in a wide range of formats after 'synthesize_texture.py'. Note that neither of the following flags can be used when using the synthesis script: '--result_only' '--raster_only'",
epilog='If there are any issues or questions, feel free to visit our GitHub repository at '
'https://github.com/ChrisPGraphics/ByExampleSynthesisOfVectorTextures'
)
parser.add_argument(
'input_path',
help="The path to the directory to convert. "
"Can either be an analyzed in the 'intermediate' directory or synthetic result in the 'output' directory."
)
parser.add_argument(
'output_path',
help="The path to the converted output file"
)
parser.add_argument(
'--format', default='infer', choices=['xml', 'json', 'svg', 'raster', 'infer'],
help="The logging level to run the script as"
)
parser.add_argument(
'--intermediate_directory',
help="The path to the intermediate directory to store converted files"
)
args = parser.parse_args()
if args.format == "infer":
logging.info("Inferring desired export format")
extension = os.path.splitext(args.output_path)[1]
try:
args.format = EXTENSION_INFERENCE[extension[1:].lower()]
except KeyError:
logging.fatal("Unable to infer export format from file extension '{}'".format(extension))
sys.exit(1)
if args.format == "xml":
exporter = XMLExporter()
elif args.format == "json":
exporter = JSONExporter()
elif args.format == "svg":
exporter = SVGExporter()
elif args.format == "raster":
exporter = RasterExporter()
else:
logging.fatal("'{}' is an unrecognized export format".format(args.format))
sys.exit(1)
logging.info("Using {}".format(exporter.get_name()))
exporter(args.input_path, args.output_path)