-
Notifications
You must be signed in to change notification settings - Fork 4
/
dzconverter.py
72 lines (42 loc) · 1.41 KB
/
dzconverter.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
import os
import re
import sys
import pyvips as vips
import json
import xmltodict
def _dzconvert_aperio(filename, outname):
try:
img = vips.Image.new_from_file(filename)
except:
print("Could not open file {0}".format(filename))
raise
mpp_meta = img.get('aperio.MPP')
basename = os.path.splitext(filename)[0]
basedir = os.path.dirname(basename)
dzfname = os.path.join(basedir, outname)
img.dzsave(dzfname)
with open(dzfname + ".dzi", 'r') as fp:
data_dict = xmltodict.parse(fp.read())
json_data = json.dumps(data_dict)
json_data = json.loads(re.sub('@+', '', json_data))
json_data["Image"]["mpp"] = mpp_meta
with open(dzfname + ".dzi", 'w') as fp:
fp.write(json.dumps(json_data))
os.rename(dzfname + "_files", dzfname + ".dzi" + "_files")
def _dzconvert_mirax(filename, outname):
try:
img = vips.Image.new_from_file(filename)
except:
print("Could not open file {0}".format(filename))
raise
def dzconvert(filename, outname):
if filename.endswith('.svs'):
_dzconvert_aperio(filename, outname)
if filename.endswith('.mrxs'):
_dzconvert_mirax(filename, outname)
if __name__ == "__main__":
if len(sys.argv) < 3:
print("Usage: dzconverter.py <input_image> <output_name>")
sys.exit(1)
else:
dzconvert(sys.argv[1], sys.argv[2])