ITK  6.0.0
Insight Toolkit
SphinxExamples/src/IO/GDCM/ReadDICOMSeriesAndWrite3DImage/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 os
19 import itk
20 import argparse
21 
22 parser = argparse.ArgumentParser(description="Read DICOM Series And Write 3D Image.")
23 parser.add_argument(
24  "dicom_directory",
25  nargs="?",
26  help="If DicomDirectory is not specified, current directory is used",
27 )
28 parser.add_argument("output_image", nargs="?")
29 parser.add_argument("series_name", nargs="?")
30 args = parser.parse_args()
31 
32 # current directory by default
33 dirName = "."
34 if args.dicom_directory:
35  dirName = args.dicom_directory
36 
37 PixelType = itk.ctype("signed short")
38 Dimension = 3
39 
40 ImageType = itk.Image[PixelType, Dimension]
41 
42 namesGenerator = itk.GDCMSeriesFileNames.New()
43 namesGenerator.SetUseSeriesDetails(True)
44 namesGenerator.AddSeriesRestriction("0008|0021")
45 namesGenerator.SetGlobalWarningDisplay(False)
46 namesGenerator.SetDirectory(dirName)
47 
48 seriesUID = namesGenerator.GetSeriesUIDs()
49 
50 if len(seriesUID) < 1:
51  print("No DICOMs in: " + dirName)
52  sys.exit(1)
53 
54 print("The directory: " + dirName)
55 print("Contains the following DICOM Series: ")
56 for uid in seriesUID:
57  print(uid)
58 
59 seriesFound = False
60 for uid in seriesUID:
61  seriesIdentifier = uid
62  if args.series_name:
63  seriesIdentifier = args.series_name
64  seriesFound = True
65  print("Reading: " + seriesIdentifier)
66  fileNames = namesGenerator.GetFileNames(seriesIdentifier)
67 
68  reader = itk.ImageSeriesReader[ImageType].New()
69  dicomIO = itk.GDCMImageIO.New()
70  reader.SetImageIO(dicomIO)
71  reader.SetFileNames(fileNames)
72  reader.ForceOrthogonalDirectionOff()
73 
74  writer = itk.ImageFileWriter[ImageType].New()
75  outFileName = os.path.join(dirName, seriesIdentifier + ".nrrd")
76  if args.output_image:
77  outFileName = args.output_image
78  writer.SetFileName(outFileName)
79  writer.UseCompressionOn()
80  writer.SetInput(reader.GetOutput())
81  print("Writing: " + outFileName)
82  writer.Update()
83 
84  if seriesFound:
85  break
itk::ImageSeriesReader
Data source that reads image data from a series of disk files.
Definition: itkImageSeriesReader.h:45
itk::ImageFileWriter
Writes image data to a single file.
Definition: itkImageFileWriter.h:90
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()