-
Notifications
You must be signed in to change notification settings - Fork 5
/
Parse SVG.py
314 lines (254 loc) · 10.5 KB
/
Parse SVG.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# This code will take an SVG created by the Geometrize web demo (https://www.samcodes.co.uk/project/geometrize-haxe-web/)
# Then extract each component object, writing a csv of polygon data which can be visualized in Tableau.
# The program will also write a csv with Tableau color palette information.
#
# Written by Ken Flerlage, November, 2017
# This is my first ever Python program, so be easy on me!!
#
# Instructions:
#
# This code is in the public domain
from xml.dom import minidom
from base64 import b16encode
import math
import sys
import os
from tkinter import filedialog
from tkinter import messagebox
from tkinter import *
# Prompt for the input file.
def get_file():
root = Tk()
root.filename = filedialog.askopenfilename(initialdir = "/",title = "Select File",filetypes = (("Scalable Vector Graphic","*.svg"),("All Files","*.*")))
root.withdraw()
return root.filename
# Convert RGB Color Values to Hex.
def convertRGBtoHex (Red, Green, Blue):
triplet = (Red, Green, Blue)
colorHex = b'#'+ b16encode(bytes(triplet))
colorHex = str(colorHex)
colorHex = colorHex[2:len(colorHex)-1]
return colorHex;
# Get the Hex color from the fill color string for the svg object.
def getColorHex (RGBString):
RGBString = RGBString[4:len(RGBString)-1]
rgbList = RGBString.split(',')
Red = int(rgbList[0])
Green = int(rgbList[1])
Blue = int(rgbList[2])
colorHex = str(convertRGBtoHex(Red, Green, Blue))
return colorHex;
# Use parametric equations to generate 100 points for an ellipse.
def buildEllipsePoints (shapeType, shapeCounter, colorHex, centerX, centerY, radiusA, radiusB, rotation):
rotationRad = math.radians(rotation)
for i in range(100):
# t range will be 0 to 2 Pi.
t = i * 2 * math.pi / 100
x = centerX + (radiusA*math.cos(t)*math.cos(rotationRad)) - (radiusB*math.sin(t)*math.sin(rotationRad))
y = centerY + (radiusA*math.cos(t)*math.sin(rotationRad)) + (radiusB*math.sin(t)*math.cos(rotationRad))
outString = shapeType + ',' + str(shapeCounter) + ',' + str(i+1) + ',' + str(x) + ',' + str(y) + ',' + colorHex
out.write (outString)
out.write('\n')
return '';
# Process a Circle Object.
def processCircleObject (xmlString, shapeCounter):
xmldoc = minidom.parseString(xmlString + '>')
itemlist = xmldoc.getElementsByTagName('circle')
s = itemlist[0]
colorHex = getColorHex(s.attributes['fill'].value)
outString = 'Circle,' + str(shapeCounter) + ',' + colorHex + ','+ '<color>' + colorHex + '</color>'
outColor.write (outString)
outColor.write('\n')
# Get all the values we need.
centerX = int(s.attributes['cx'].value)
centerY = int(s.attributes['cy'].value)
radiusA = int(s.attributes['r'].value)
radiusB = radiusA
rotation = 0
buildEllipsePoints ('Circle', shapeCounter, colorHex, centerX, centerY, radiusA, radiusB, rotation)
return '';
# Process a Rectangle Object
def processRectangleObject (xmlString, shapeCounter):
xmldoc = minidom.parseString(xmlString + '>')
itemlist = xmldoc.getElementsByTagName('rect')
s = itemlist[0]
colorHex = getColorHex(s.attributes['fill'].value)
outString = 'Rectangle,' + str(shapeCounter) + ',' + colorHex + ','+ '<color>' + colorHex + '</color>'
outColor.write (outString)
outColor.write('\n')
# Get x & y which will give us the first point (top left).
# Then get height and width which will give us the rest of the points.
x = int(s.attributes['x'].value)
y = int(s.attributes['y'].value)
height = int(s.attributes['height'].value)
width = int(s.attributes['width'].value)
# Write point # 1
outString = 'Rectangle,' + str(shapeCounter) + ',1,' + str(x) + ',' + str(y) + ',' + colorHex
out.write (outString)
out.write('\n')
# Write point # 2
outString = 'Rectangle,' + str(shapeCounter) + ',2,' + str(x+width) + ',' + str(y) + ',' + colorHex
out.write (outString)
out.write('\n')
# Write point # 3
outString = 'Rectangle,' + str(shapeCounter) + ',3,' + str(x+width) + ',' + str(y+height) + ',' + colorHex
out.write (outString)
out.write('\n')
# Write point # 4
outString = 'Rectangle,' + str(shapeCounter) + ',4,' + str(x) + ',' + str(y+height) + ',' + colorHex
out.write (outString)
out.write('\n')
return '';
# Process a Regular Ellipse Object (one with no rotation.)
def processRegularEllipseObject (xmlString, shapeCounter):
xmldoc = minidom.parseString(xmlString + '>')
itemlist = xmldoc.getElementsByTagName('ellipse')
s = itemlist[0]
colorHex = getColorHex(s.attributes['fill'].value)
outString = 'Ellipse,' + str(shapeCounter) + ',' + colorHex + ','+ '<color>' + colorHex + '</color>'
outColor.write (outString)
outColor.write('\n')
# Get all the values we need.
centerX = int(s.attributes['cx'].value)
centerY = int(s.attributes['cy'].value)
radiusA = int(s.attributes['rx'].value)
radiusB = int(s.attributes['ry'].value)
rotation = 0
buildEllipsePoints ('Circle', shapeCounter, colorHex, centerX, centerY, radiusA, radiusB, rotation)
return '';
# Process a transformed ellipse (one with rotation).
def processEllipseObject (xmlString, shapeCounter):
xmldoc = minidom.parseString(xmlString + '>')
# Get transformation details first.
itemlist = xmldoc.getElementsByTagName('g')
s = itemlist[0]
transform = s.attributes['transform'].value
# Initialize variables.
transX = 0
transY = 0
rotation = 0
scaleX = 1
scaleY = 1
pos = transform.find('translate')
if pos >=0:
# Get Translation Details (this will give us an adjusted center point)
translation = transform[pos+10:]
pos2 = translation.find(')')
translation = translation[:pos2]
transList = translation.split(' ')
transX = int(transList[0])
transY = int(transList[1])
pos = transform.find('rotate')
if pos >=0:
# Process Rotation
rotation = transform[pos+7:]
pos2 = rotation.find(')')
rotation = int(rotation[:pos2])
pos = transform.find('scale')
if pos >=0:
# Process Scale (this will give us adjusted radiuses)
scale = transform[pos+6:]
pos2 = scale.find(')')
scale = scale[:pos2]
scaleList = scale.split(' ')
scaleX = int(scaleList[0])
scaleY = int(scaleList[1])
# Now get ellipse details.
itemlist = xmldoc.getElementsByTagName('ellipse')
s = itemlist[0]
colorHex = getColorHex(s.attributes['fill'].value)
outString = 'Ellipse,' + str(shapeCounter) + ',' + colorHex + ','+ '<color>' + colorHex + '</color>'
outColor.write (outString)
outColor.write('\n')
centerX = int(s.attributes['cx'].value)
centerY = int(s.attributes['cy'].value)
radiusA = int(s.attributes['rx'].value)
radiusB = int(s.attributes['ry'].value)
buildEllipsePoints ('Ellipse', shapeCounter, colorHex, centerX+transX, centerY+transY, radiusA*scaleX, radiusB*scaleY, rotation)
return '';
# Process a polygon object (could be rotated rectangles or triangles)
def processPolygonObject (xmlString, shapeCounter):
xmldoc = minidom.parseString(xmlString + '>')
itemlist = xmldoc.getElementsByTagName('polygon')
s = itemlist[0]
colorHex = getColorHex(s.attributes['fill'].value)
outString = 'Polygon,' + str(shapeCounter) + ',' + colorHex + ','+ '<color>' + colorHex + '</color>'
outColor.write (outString)
outColor.write('\n')
# Get and parse the points. Some objects separate x and y with space and others with comma, so we'll make them consistent.
points = s.attributes['points'].value
points = points.replace(',',' ')
pointList = points.split(' ')
# Loop through all the points in the polygon.
pointCounter = 1
pointIsX = True
for p in pointList:
if pointIsX == True:
x = p
pointIsX = False
else:
y = p
pointIsX = True
outString = 'Polygon,' + str(shapeCounter) + ',' + str(pointCounter) + ',' + str(x) + ',' + str(y) + ',' + colorHex
out.write (outString)
out.write('\n')
pointCounter += 1
return '';
# Main processing routine.
# Prompt for SVG file.
xmlin = get_file()
if xmlin == "":
messagebox.showinfo("Error", "No file selected. Program will now quit.")
sys.exit()
# Set output files to write to the same folder.
filepath = os.path.dirname(xmlin)
if filepath[-1:] != "/":
filepath += "/"
outFile = filepath + 'Polygon.csv'
colorFile = filepath + 'Colors.csv'
out = open(outFile,'w')
outColor = open(colorFile,'w')
# Write header of the polygon csv file.
outString = 'Shape,Polygon ID,Point ID,X,Y,Color Hex'
out.write (outString)
out.write('\n')
# Write header of the color csv file.
outString = 'Shape,Polygon ID,Color Hex,Palette'
outColor.write (outString)
outColor.write('\n')
xmldoc = minidom.parse(xmlin)
shapeNum = 1
# Loop through each line of the file and parse it into xml components
with open(xmlin) as f:
lines = f.readlines()
for line in lines:
linesplit = line.split('>,')
for linex in linesplit:
linex = linex.replace('>]</svg>','')
# If the first character is [, remove it (this is the first line of the file)
if linex[0:1]=='[':
linex=linex[1:]
# This is a circle.
if linex[0:7]=='<circle':
processCircleObject(linex, shapeNum)
shapeNum += 1
# This is a rectangle
if linex[0:5]=='<rect':
processRectangleObject(linex, shapeNum)
shapeNum += 1
# This is a transformed ellipse.
if linex[0:12]=='<g transform':
processEllipseObject(linex, shapeNum)
shapeNum += 1
# This is a regular, non-transformed ellipse.
if linex[0:8]=='<ellipse':
processRegularEllipseObject(linex, shapeNum)
shapeNum += 1
# This is a triangle or rotated rectangle.
if linex[0:8]=='<polygon':
processPolygonObject(linex, shapeNum)
shapeNum += 1
out.close()
outColor.close()
f.close
messagebox.showinfo('Complete', 'Output files, polygon.csv and colors.csv have been written to the following directory: ' + filepath)