This repository has been archived by the owner on Jun 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
/
dcm_qt_tree.py
104 lines (87 loc) · 3.06 KB
/
dcm_qt_tree.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
# dcm_qt_tree.py
"""View DICOM files in a tree using Qt and PySide"""
# Copyright (c) 2013 Padraig Looney
# This file is released under the
# pydicom (https://github.com/pydicom/pydicom)
# license, see the file LICENSE available at
# (https://github.com/pydicom/pydicom)
import pydicom
import sys
from PySide import QtGui
import collections
class DicomTree(object):
def __init__(self, filename):
self.filename = filename
def show_tree(self):
ds = self.dicom_to_dataset(self.filename)
dic = self.dataset_to_dic(ds)
model = self.dic_to_model(dic)
self.display(model)
def array_to_model(self, array):
model = QtGui.QStandardItemModel()
parentItem = model.invisibleRootItem()
for ntuple in array:
tag = ntuple[0]
value = ntuple[1]
if isinstance(value, dict):
self.recurse_dic_to_item(value, parentItem)
else:
item = QtGui.QStandardItem(tag + str(value))
parentItem.appendRow(item)
return parentItem
def dic_to_model(self, dic):
model = QtGui.QStandardItemModel()
parentItem = model.invisibleRootItem()
self.recurse_dic_to_item(dic, parentItem)
return model
def dataset_to_array(self, dataset):
array = []
for data_element in dataset:
array.append(self.data_element_to_dic(data_element))
return array
def recurse_dic_to_item(self, dic, parent):
for k in dic:
v = dic[k]
if isinstance(v, dict):
item = QtGui.QStandardItem(k + ':' + str(v))
parent.appendRow(self.recurse_dic_to_item(v, item))
else:
item = QtGui.QStandardItem(k + ': ' + str(v))
parent.appendRow(item)
return parent
def dicom_to_dataset(self, filename):
dataset = pydicom.dcmread(filename, force=True)
return dataset
def data_element_to_dic(self, data_element):
dic = collections.OrderedDict()
if data_element.VR == "SQ":
items = collections.OrderedDict()
dic[data_element.name] = items
i = 0
for dataset_item in data_element:
items['item ' + str(i)] = self.dataset_to_dic(dataset_item)
i += 1
elif data_element.name != 'Pixel Data':
dic[data_element.name] = data_element.value
return dic
def dataset_to_dic(self, dataset):
dic = collections.OrderedDict()
for data_element in dataset:
dic.update(self.data_element_to_dic(data_element))
return dic
def display(self, model):
app = QtGui.QApplication.instance()
# create QApplication if it doesnt exist
if not app:
app = QtGui.QApplication(sys.argv)
tree = QtGui.QTreeView()
tree.setModel(model)
tree.show()
app.exec_()
return tree
def main():
filename = sys.argv[1]
dicomTree = DicomTree(filename)
dicomTree.show_tree()
if __name__ == "__main__":
main()