-
Notifications
You must be signed in to change notification settings - Fork 2
/
xyz-to-json.py
54 lines (39 loc) · 1.46 KB
/
xyz-to-json.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
"""
This script converts NeSys .xyz files containing in-brain coordinates
to MeshView JSON format (https://meshview.apps.hbp.eu/).
"""
import os
import json
def parse_xyz(fname):
jsondata = []
obj = {}
triplets = []
with open(fname,"r") as xyz:
for line in xyz:
line = line.strip()
if line.startswith("SCALE"):
scale = int(line[6:])
elif line.startswith("#"):
if triplets:
obj["triplets"] = triplets
jsondata.append(obj)
obj = {}
triplets = []
obj["name"] = line[1:]
obj["scale"] = scale
elif line.startswith("RGB"):
obj["r"] = int(float(line.split()[1])*255)
obj["g"] = int(float(line.split()[2])*255)
obj["b"] = int(float(line.split()[3])*255)
elif line == "":
pass
else:
triplets.extend([float(a) for a in line.split(" ")])
return(jsondata)
if __name__=='__main__':
os.chdir(os.path.dirname(os.path.realpath(__file__)))
xyzfiles = [f for f in os.listdir(os.getcwd()) if f.endswith(".xyz")]
for file in xyzfiles:
result = parse_xyz(file)
with open(file.replace(".xyz",".json"),"w") as output:
json.dump(result,output)