GDCM: ReadAndDumpDICOMDIR.py

GDCM 3.2.10

ReadAndDumpDICOMDIR.py
1
23
24
25
26import sys
27import gdcm
28
29if __name__ == "__main__":
30 # Check arguments
31 if (len(sys.argv) < 2):
32 # No filename passed
33 print "No input filename found"
34 quit()
35
36 filename = sys.argv[1]
37
38
39 # Read file
40 reader = gdcm.Reader()
41 reader.SetFileName(filename)
42 if (not reader.Read()):
43 print "Unable to read %s" % (filename)
44 quit()
45
46 file = reader.GetFile()
47
48 # Retrieve header information
49 fileMetaInformation = file.GetHeader()
50 print fileMetaInformation
51
52 # Retrieve data set
53 dataSet = file.GetDataSet()
54 #print dataSet
55
56 # Check media storage
57 mediaStorage = gdcm.MediaStorage()
58 mediaStorage.SetFromFile(file)
59 if (gdcm.MediaStorage.GetMSType(str(mediaStorage)) != gdcm.MediaStorage.MediaStorageDirectoryStorage):
60 # File is not a DICOMDIR
61 print "This file is not a DICOMDIR (Media storage type: %s)" % (str(mediaStorage))
62 quit()
63
64 # Check Media Storage SOP Class
65 if (fileMetaInformation.FindDataElement(gdcm.Tag(0x0002, 0x0002))):
66 sopClassUid = str(fileMetaInformation.GetDataElement(gdcm.Tag(0x0002, 0x0002)).GetValue())
67 # Check SOP UID
68 if (sopClassUid != "1.2.840.10008.1.3.10"):
69 # File is not a DICOMDIR
70 print "This file is not a DICOMDIR"
71 else:
72 # Not present
73 print "Media Storage SOP Class not present"
74 quit()
75
76 # Iterate through the DICOMDIR data set
77 iterator = dataSet.GetDES().begin()
78 while (not iterator.equal(dataSet.GetDES().end())):
79 dataElement = iterator.next()
80
81 # Check the element tag
82 if (dataElement.GetTag() == gdcm.Tag(0x004, 0x1220)):
83 # The 'Directory Record Sequence' element
84 sequence = dataElement.GetValueAsSQ()
85
86 # Loop through the sequence items
87 itemNr = 1
88 while (itemNr < sequence.GetNumberOfItems()):
89 item = sequence.GetItem(itemNr)
90
91 # Check the element tag
92 if (item.FindDataElement(gdcm.Tag(0x0004, 0x1430))):
93 # The 'Directory Record Type' element
94 value = str(item.GetDataElement(gdcm.Tag(0x0004, 0x1430)).GetValue())
95
96 # PATIENT
97 while (value.strip() == "PATIENT"):
98 print value.strip()
99 # Print patient name
100 if (item.FindDataElement(gdcm.Tag(0x0010, 0x0010))):
101 value = str(item.GetDataElement(gdcm.Tag(0x0010, 0x0010)).GetValue())
102 print value
103
104 # Print patient ID
105 if (item.FindDataElement(gdcm.Tag(0x0010, 0x0020))):
106 value = str(item.GetDataElement(gdcm.Tag(0x0010, 0x0020)).GetValue())
107 print value
108
109 # Next
110 itemNr = itemNr + 1
111 item = sequence.GetItem(itemNr)
112 if (item.FindDataElement(gdcm.Tag(0x0004, 0x1430))):
113 value = str(item.GetDataElement(gdcm.Tag(0x0004, 0x1430)).GetValue())
114
115 # STUDY
116 while (value.strip() == "STUDY"):
117 print value.strip()
118
119 # Print study UID
120 if (item.FindDataElement(gdcm.Tag(0x0020, 0x000d))):
121 value = str(item.GetDataElement(gdcm.Tag(0x0020, 0x000d)).GetValue())
122 print value
123
124 # Print study date
125 if (item.FindDataElement(gdcm.Tag(0x0008, 0x0020))):
126 value = str(item.GetDataElement(gdcm.Tag(0x0008, 0x0020)).GetValue())
127 print value
128
129 # Print study description
130 if (item.FindDataElement(gdcm.Tag(0x0008, 0x1030))):
131 value = str(item.GetDataElement(gdcm.Tag(0x0008, 0x1030)).GetValue())
132 print value
133
134 # Next
135 itemNr = itemNr + 1
136 item = sequence.GetItem(itemNr)
137 if (item.FindDataElement(gdcm.Tag(0x0004, 0x1430))):
138 value = str(item.GetDataElement(gdcm.Tag(0x0004, 0x1430)).GetValue())
139
140 # SERIES
141 while (value.strip() == "SERIES"):
142 print value.strip()
143
144 # Print series UID
145 if (item.FindDataElement(gdcm.Tag(0x0020, 0x000e))):
146 value = str(item.GetDataElement(gdcm.Tag(0x0020, 0x000e)).GetValue())
147 print value
148
149 # Print series modality
150 if (item.FindDataElement(gdcm.Tag(0x0008, 0x0060))):
151 value = str(item.GetDataElement(gdcm.Tag(0x0008, 0x0060)).GetValue())
152 print "Modality"
153 print value
154
155 # Print series description
156 if (item.FindDataElement(gdcm.Tag(0x0008, 0x103e))):
157 value = str(item.GetDataElement(gdcm.Tag(0x0008, 0x103e)).GetValue())
158 print "Description"
159 print value
160
161 # Next
162 itemNr = itemNr + 1
163 item = sequence.GetItem(itemNr)
164 if (item.FindDataElement(gdcm.Tag(0x0004, 0x1430))):
165 value = str(item.GetDataElement(gdcm.Tag(0x0004, 0x1430)).GetValue())
166
167 # IMAGE
168 while (value.strip() == "IMAGE"):
169 print value.strip()
170
171 # Print image UID
172 if (item.FindDataElement(gdcm.Tag(0x0004, 0x1511))):
173 value = str(item.GetDataElement(gdcm.Tag(0x0004, 0x1511)).GetValue())
174 print value
175
176 # Next
177 if (itemNr < sequence.GetNumberOfItems()):
178 itemNr = itemNr + 1
179 else:
180 break
181
182 item = sequence.GetItem(itemNr)
183 if (item.FindDataElement(gdcm.Tag(0x0004, 0x1430))):
184 value = str(item.GetDataElement(gdcm.Tag(0x0004, 0x1430)).GetValue())
185
186 # Next
187 itemNr = itemNr + 1
gdcm::MediaStorage
MediaStorage.
Definition gdcmMediaStorage.h:44
gdcm::MediaStorage::GetMSType
static MSType GetMSType(const char *str)
gdcm::Reader
Reader ala DOM (Document Object Model).
Definition gdcmReader.h:54
gdcm::Tag
Class to represent a DICOM Data Element (Attribute) Tag (Group, Element).
Definition gdcmTag.h:39