-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayerExport.py
More file actions
executable file
·167 lines (134 loc) · 7.19 KB
/
LayerExport.py
File metadata and controls
executable file
·167 lines (134 loc) · 7.19 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
#!python
"""
Export selected layers from Inkscape SVG to PNG.
"""
import base64
from xml.dom import minidom
import codecs
import sys
from optparse import OptionParser
import os
import logging
import subprocess
from tqdm import tqdm
# logging setup
handler = logging.StreamHandler(stream=sys.stdout)
formatter = logging.Formatter(fmt="%(asctime)s %(levelname)s: %(message)s")
handler.setFormatter(formatter)
logger = logging.Logger(name=__file__, level=logging.WARN)
logger.addHandler(handler)
class Exporter(object):
def __init__(self, source=None, output=None, dpi=90, keep_svg=False, base64=False):
self.source = source
self.output_dir = output
self.dpi = dpi
self.keep_svg = keep_svg
self.base64 = base64
self.inkscape_exe = "C:/Program Files (x86)/Inkscape/inkscape.exe"
if not os.path.exists(os.path.abspath(self.output_dir)):
os.mkdir(os.path.abspath(self.output_dir))
logging.info("created output directory %s" % os.path.abspath(self.output_dir))
path, file = os.path.split(os.path.abspath(self.source))
self.sourcename, self.ext = os.path.splitext(file)
def process(self):
svg = minidom.parse(open(self.source))
g_base = None
g_top = None
g_render = list()
# find all layers
for g in svg.getElementsByTagName("g"):
if not "inkscape:groupmode" in g.attributes.keys():
continue
if not g.attributes["inkscape:groupmode"].value == "layer":
continue
if "inkscape:label" in g.attributes.keys():
label = str(g.attributes["inkscape:label"].value).lower()
if "base" in label:
logger.debug("found base layer with name {}".format(label))
g_base = g
elif "top" in label:
logger.debug("found top layer with name {}".format(label))
g_top = g
else:
g_render.append(g)
# may be we found no base layer..
if g_base is None:
logger.error("Failed to find Base-Layer.. quitting!")
exit()
for i in tqdm(range(len(g_render))):
svg = minidom.parse(open(self.source))
root = svg.documentElement
for g in svg.getElementsByTagName("g"):
if not "inkscape:groupmode" in g.attributes.keys():
continue
if not g.attributes["inkscape:groupmode"].value == "layer":
continue
if "inkscape:label" in g.attributes.keys():
label = str(g.attributes["inkscape:label"].value).lower()
if g_base.attributes["inkscape:label"].value == label:
# display base layer always
g.attributes['style'] = "display:inline"
elif g_top is not None and g_top.attributes["inkscape:label"].value == label:
# display base layer always
g.attributes['style'] = "display:inline"
else:
if g_render[i].attributes["inkscape:label"].value == label:
# keep it
g.attributes['style'] = "display:inline"
else:
logger.debug("attributes: %s" % str(g.attributes.keys()))
for attr in g.attributes.keys():
logger.debug("%s -> %s" % (attr, g.attributes[attr].value))
logger.debug("removing %s" % str(g.attributes["inkscape:label"].value))
# remove it
root.removeChild(g)
export = svg.toxml()
label = g_render[i].attributes["inkscape:label"].value
pngfile = str("%s_%s_%ddpi.png" % (self.sourcename, label, self.dpi))
svgfile = str("%s_%s.svg" % (self.sourcename, label))
codecs.open(os.path.join(os.path.abspath(self.output_dir), svgfile), "w", encoding="utf8").write(export)
inkscape_export_command = [
self.inkscape_exe,
"--without-gui",
"--export-area-page", # TODO: plug in the mode from options
"--export-png=%s" % os.path.join(self.output_dir, pngfile),
"--export-dpi=%d" % self.dpi,
#"-b 0xffffffff",
os.path.join(os.path.abspath(self.output_dir), svgfile)
]
shell_output = subprocess.check_output(inkscape_export_command)
logger.debug("----- Inkscape-Output -----\n\n%s" % shell_output.decode())
if not self.keep_svg:
os.remove(os.path.join(os.path.abspath(self.output_dir), svgfile))
if self.base64:
txt_file = "{}.base64".format(os.path.splitext(os.path.join(self.output_dir, pngfile))[0])
encoded_string = None
with open(os.path.join(self.output_dir, pngfile), "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
if encoded_string is None:
logger.debug("error converting file {}".format(os.path.join(self.output_dir, pngfile)))
return
with open(txt_file, "w") as base64_file:
base64_file.write(encoded_string.decode())
logger.debug("successfully converted {} to {}".format(os.path.join(self.output_dir, pngfile), txt_file))
if __name__ == "__main__":
parser = OptionParser()
parser.add_option("-i", "--input", dest="input", help="input file to process", metavar="FILE")
parser.add_option("-o", "--output", dest="output", help="output directory", metavar="OUTDIR")
parser.add_option("-k", "--keepsvg", dest="keep_svg", help="keep svg file", action="store_true")
parser.add_option("-b", "--base64", dest="base64", help="output image as base64", action="store_true", default=False)
parser.add_option("-d", "--dpi", dest="dpi", help="specify resolution in dpi, e.g. --dpi 90", type=int, default=90)
parser.add_option("-v", "--verbose", dest="verbose", help="enable debug output", action="store_true", default=False)
# parser.add_option("-l", "--layers", dest="layers", help="the layers to export, e.g. --layers foo,bar,baz")
# parser.add_option("-m", "--mode", dest="mode", help="select mode, e.g. --mode page (default)", default="page")
# parser.add_option("-r", "--resolution", dest="resolution", help="specify resolution in px, e.g. --resolution 80x80")
(options, args) = parser.parse_args()
if options.verbose:
logger.setLevel(logging.DEBUG)
logger.debug("Raw Arguments: %s" % sys.argv)
if options.output is None or options.input is None:
logger.error("params input and output are required!")
parser.print_help()
exit()
e = Exporter(source=options.input, output=options.output, dpi=options.dpi, keep_svg=options.keep_svg, base64=options.base64)
e.process()