-
Notifications
You must be signed in to change notification settings - Fork 1
/
Convert.py
47 lines (45 loc) · 1.59 KB
/
Convert.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
from bs4 import BeautifulSoup
import os
# function to convert xml to scgink
def Convert(fname):
rootpt = os.getcwd()+'/'
name = fname.strip('.xml')
with open('{}Input/{}'.format(rootpt, fname), "r") as f:
contents = f.read()
soup = BeautifulSoup(contents, 'lxml')
pages = {}
pagenum = 0
for tag in soup.find_all('page'):
pages[pagenum] = {}
stroke_counter = 0
for stroke in tag.find_all('stroke'):
text = str(stroke.text).split(' ')
x = []
y = []
for i in range(len(text)):
if i % 2 == 0:
x.append(float(text[i]))
else:
y.append(float(text[i]))
coordinates = {}
coordinates['x'] = x
coordinates['y'] = y
pages[pagenum][stroke_counter] = coordinates
stroke_counter += 1
pagenum += 1
f.close()
# write strokes content into scgink format
for num in pages.keys():
output = '{}SCG/{}_{}.scgink'.format(rootpt, name, num)
newf = open(output, "w+")
newf.write('SCG_INK\n')
allstrokes = pages[num]
newf.write(str(len(allstrokes)) + '\n')
for i in range(len(allstrokes)):
stroke = allstrokes[i]
pts = len(stroke['x'])
newf.write(str(pts) + '\n')
for j in range(pts):
newf.write(str(stroke['x'][j]) + ' ' + str(stroke['y'][j]) + '\n')
newf.close()
return len(pages)