ITK  5.4.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 # https://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 import argparse
20 
21 parser = argparse.ArgumentParser(description="Read And Print DICOM Tags.")
22 parser.add_argument(
23  "dicom_directory",
24  nargs="?",
25  help="If DicomDirectory is not specified, current directory is used",
26 )
27 args = parser.parse_args()
28 
29 # current directory by default
30 dirName = "."
31 if args.dicom_directory:
32  dirName = args.dicom_directory
33 
34 # Setup the image readers with their type
35 PixelType = itk.ctype("signed short")
36 Dimension = 3
37 
38 ImageType = itk.Image[PixelType, Dimension]
39 
40 # Using GDCMSeriesFileNames to generate the names of
41 # DICOM files.
42 namesGenerator = itk.GDCMSeriesFileNames.New()
43 namesGenerator.SetUseSeriesDetails(True)
44 namesGenerator.SetDirectory(dirName)
45 
46 # Get the names of files
47 fileNames = namesGenerator.GetInputFileNames()
48 
49 # Setup the image series reader using GDCMImageIO
50 reader = itk.ImageSeriesReader[ImageType].New()
51 dicomIO = itk.GDCMImageIO.New()
52 dicomIO.LoadPrivateTagsOn()
53 reader.SetImageIO(dicomIO)
54 reader.SetFileNames(fileNames)
55 
56 # Attempt to read the series, exit if unable to.
57 try:
58  reader.Update()
59 except:
60  print("Error occured while reading DICOMs in: " + dirName)
61  sys.exit(1)
62 
63 # ITK internally queries GDCM and obtains all the DICOM tags from the file
64 # headers. The tag values are stored in the MetaDataDictionary
65 # which is a general-purpose container for \{key,value\} pairs. The Metadata
66 # dictionary can be recovered from any ImageIO class by invoking the
67 # GetMetaDataDictionary() method.
68 metadata = dicomIO.GetMetaDataDictionary()
69 
70 # Print the key value pairs from the metadadictionary
71 tagkeys = metadata.GetKeys()
72 
73 for tagkey in tagkeys:
74  # Note the [] operator for the key
75  try:
76  tagvalue = metadata[tagkey]
77  print(tagkey + "=" + str(tagvalue))
78  except RuntimeError:
79  # Cannot pass specialized values into metadata dictionary.
80  print("Cannot pass specialized value" + tagkey + "into metadadictionary")
81 
82 
83 # Illustrating use of getting a label given a tag here
84 entryID = "0010|0010"
85 if not metadata.HasKey(entryID):
86  print("tag: " + entryID + " not found in series")
87 else:
88  # The second parameter is mandatory in python to get the
89  # string label value
90  label = itk.GDCMImageIO.GetLabelFromTag(entryID, "")
91  tagvalue = metadata[entryID]
92  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:88
New
static Pointer New()