ITK  5.2.0
Insight Toolkit
SphinxExamples/src/IO/GDCM/ReadAndPrintDICOMTags/Code.py
1 #!/usr/bin/env python
2 
3 # Copyright NumFOCUS
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0.txt
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 
17 import sys
18 import itk
19 
20 if len(sys.argv) != 2:
21  print("Usage: " + sys.argv[0] +
22  " [DicomDirectory]")
23  print("If DicomDirectory is not specified, current directory is used\n")
24 
25 # current directory by default
26 dirName = '.'
27 if len(sys.argv) > 1:
28  dirName = sys.argv[1]
29 
30 # Setup the image readers with their type
31 PixelType = itk.ctype('signed short')
32 Dimension = 3
33 
34 ImageType = itk.Image[PixelType, Dimension]
35 
36 # Using GDCMSeriesFileNames to generate the names of
37 # DICOM files.
38 namesGenerator = itk.GDCMSeriesFileNames.New()
39 namesGenerator.SetUseSeriesDetails(True)
40 namesGenerator.SetDirectory(dirName)
41 
42 # Get the names of files
43 fileNames = namesGenerator.GetInputFileNames()
44 
45 # Setup the image series reader using GDCMImageIO
46 reader = itk.ImageSeriesReader[ImageType].New()
47 dicomIO = itk.GDCMImageIO.New()
48 dicomIO.LoadPrivateTagsOn()
49 reader.SetImageIO(dicomIO)
50 reader.SetFileNames(fileNames)
51 
52 # Attempt to read the series, exit if unable to.
53 try:
54  reader.Update()
55 except:
56  print('Error occured while reading DICOMs in: ' + dirName)
57  sys.exit(1)
58 
59 # ITK internally queries GDCM and obtains all the DICOM tags from the file
60 # headers. The tag values are stored in the MetaDataDictionary
61 # which is a general-purpose container for \{key,value\} pairs. The Metadata
62 # dictionary can be recovered from any ImageIO class by invoking the
63 # GetMetaDataDictionary() method.
64 metadata = dicomIO.GetMetaDataDictionary()
65 
66 # Print the key value pairs from the metadadictionary
67 tagkeys = metadata.GetKeys()
68 
69 for tagkey in tagkeys:
70  # Note the [] operator for the key
71  try:
72  tagvalue = metadata[tagkey]
73  print(tagkey + '=' + str(tagvalue))
74  except RuntimeError:
75  #Cannot pass specialized values into metadata dictionary.
76  print("Cannot pass specialized value" + tagkey + "into metadadictionary")
77 
78 
79 # Illustrating use of getting a label given a tag here
80 entryID = "0010|0010"
81 if not metadata.HasKey(entryID):
82  print('tag: ' + entryID + ' not found in series')
83 else:
84  # The second parameter is mandatory in python to get the
85  # string label value
86  label = itk.GDCMImageIO.GetLabelFromTag(entryID, "")
87  tagvalue = metadata[entryID]
88  print(label[1] + ' (' + entryID + ') is: ' + str(tagvalue))
itk::GDCMImageIO::GetLabelFromTag
static bool GetLabelFromTag(const std::string &tag, std::string &labelId)
itk::ImageSeriesReader
Data source that reads image data from a series of disk files.
Definition: itkImageSeriesReader.h:45
itk::GDCMSeriesFileNames::New
static Pointer New()
itk::GDCMImageIO::New
static Pointer New()
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:86