-
Notifications
You must be signed in to change notification settings - Fork 0
/
full_example.py
148 lines (129 loc) · 3.96 KB
/
full_example.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
import svgwrite
import svgelements
import numpy as np
def addToDict(ddi,topLK,subLK):
if topLK in ddi.keys():
ddi[topLK].append(subLK)
else:
ddi[topLK] = [subLK]
def buildExtra(eleAttrs, noList, custAttrs={}):
noList.extend(['tag','attributes',''])
extrakw = {}
for kw, kv in eleAttrs.items():
if kw in noList:
continue
if kv == "":
continue
kw = kw.strip().replace("-","_")
kv = kv.strip()
#Special Cases
if kw == "stroke_dasharray":
kv = [int(x.strip()) for x in kv.split(",")]
if kw in custAttrs.keys():
extrakw[kw] = custAttrs[kw]
else:
extrakw[kw] = kv
return extrakw
def addElements(svgwriteObj, elementList, **extraprops):
typeExamp = {}
drawnElements = []
for element in elementList:
eleType = element.values['tag']
eleAttrs = element.values
curEle = None
if eleType == 'line':
extrakw = buildExtra(eleAttrs, ['x1','y1','x2','y2','width','height'], extraprops)
lineS = (element.x1, element.y1)
lineE = (element.x2, element.y2)
curEle = svgwrite.shapes.Line(lineS,lineE,**extrakw)
elif eleType == 'path':
extrakw = buildExtra(eleAttrs, ['width','height','pathd_loaded','d'], extraprops)
reEl = element.reify()
pathD = reEl.d()
curEle = svgwrite.path.Path(d=pathD, **extrakw)
elif eleType == 'rect':
extrakw = buildExtra(eleAttrs, ['x','y','width','height'], extraprops)
reIn = (element.x, element.y)
reSi = (element.width, element.height)
curEle = svgwrite.shapes.Rect(insert=reIn, size=reSi, **extrakw)
elif eleType == 'polygon':
extrakw = buildExtra(eleAttrs, ['points','width','height'], extraprops)
polPo = element.points
curEle = svgwrite.shapes.Polygon(points=polPo, **extrakw)
else:
typeExamp[curEle] = eleAttrs
if curEle is not None:
if element.id is not None:
curEle.update({"id":element.id})
svgwriteObj.add(curEle)
drawnElements.append(curEle)
return typeExamp, drawnElements
def repreCircle(pointL):
xs = np.array([p[0] for p in pointL])
ys = np.array([p[1] for p in pointL])
txc = np.mean(xs)
tyc = np.mean(ys)
cirTest = np.var(np.sqrt(np.square(xs-txc) + np.square(ys-tyc)))
if cirTest < 2.0:
return True
return False
def isPathCircle(element):
elementPts = [[p.x, p.y] for p in element.as_points()]
return repreCircle(elementPts)
svg = svgelements.SVG.parse('<input filename>.svg')
elementDicts = {}
nEle = 0
for element in svg.elements():
try:
if element.values['visibility'] == 'hidden':
continue
except (KeyError, AttributeError):
pass
# An example of some processing, only keep large shapes
if isinstance(element, svgelements.Shape):
if element.values['tag'] == "path":
element = element.reify()
if len(element) < 5:
continue
eleVals = element.values
ebbox = element.bbox()
h = ebbox[3] - ebbox[1]
w = ebbox[2] - ebbox[0]
eArea = h*w
eleType = eleVals['tag']
if (eleType == "path") and (eArea > 10.0) and (eleVals['fill'] == 'none') and (eleVals['stroke'] == r"#999999"):
element.id = "small"
addToDict(elementDicts, eleType, element)
elif (eleType == "path") and (eArea > 10.0) and (not isPathCircle(element)):
element.id = "large"
addToDict(elementDicts, eleType, element)
elif (eleType == "rect") and (eArea > 10.0):
if (abs(element.width - svg.width) > 5) or (abs(element.height - svg.height) > 5):
addToDict(elementDicts, eleType, element)
else:
pass
else:
pass
D = svgwrite.drawing.Drawing(filename="cleaned_fig.svg", size=(svg.width, svg.height))
typeExamp, drawnEle = addElements(D, elementDicts['rect'], stroke='#000000', stroke_width=3, stroke_opacity=1.0)
typeExamp, drawnEle = addElements(D, elementDicts['path'])
curS = svgwrite.container.Style('''
#small {
stroke:#BBBBBB;
}
#large {
stroke:#403f47;
stroke-dasharray:4;
}
''')
D.defs.add(curS)
D.save(pretty=True)
if len(typeExamp) > 0:
print("Unable to parse every object found")
for kk, vv in typeExamp.items():
print("Type : ", kk)
print("----------------------")
print(vv)
print("")
print("======================")
print("")