SimpleITK  
Python/DicomSeriesReader2.py
1 #!/usr/bin/env python
2 
3 # =========================================================================
4 #
5 # Copyright NumFOCUS
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0.txt
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19 # =========================================================================
20 
21 
22 """ This example shows how to read a specific series from a Dicom directory that
23 may contain more than one series. The script scans for all series. If an
24 output name is given, it writes out the requested series. If no specific
25 series name is given, the first series found is written.
26 """
27 
28 
29 import sys
30 import getopt
31 import SimpleITK as sitk
32 
33 target_series = ""
34 output_image = ""
35 
36 
37 def usage():
38  """ Print usage information. """
39  print(
40  f"\nUsage: {sys.argv[0]} [-s series_name] input_directory [output_image]\n"
41  )
42 
43 
44 # Parse command line options
45 try:
46  opts, args = getopt.getopt(sys.argv[1:], "s:", ["series"])
47 except getopt.GetoptError:
48  usage()
49  sys.exit(1)
50 
51 for o, a in opts:
52  if o in ("-s", "--series"):
53  target_series = a
54  else:
55  assert False, "unhandled options"
56 
57 # Get input/output names
58 if len(args) < 1:
59  print(args)
60  usage()
61  sys.exit(1)
62 
63 input_directory = args[0]
64 if len(args) > 1:
65  output_image = args[1]
66 
67 # Find the Dicom series
68 reader = sitk.ImageSeriesReader()
69 written = False
70 
71 series_found = reader.GetGDCMSeriesIDs(input_directory)
72 
73 # Process each Dicom series
74 if len(series_found):
75 
76  for serie in series_found:
77 
78  print("\nSeries:", serie)
79 
80  # Get the Dicom filename corresponding to the current series
81  dicom_names = reader.GetGDCMSeriesFileNames(input_directory, serie)
82 
83  print("\nFiles in series: ", dicom_names)
84 
85  if len(dicom_names):
86  reader.SetFileNames(dicom_names)
87  image = reader.Execute()
88  print("\nImage size: ", image.GetSize())
89 
90  if (output_image != "") and not written:
91  if target_series in ('', serie):
92  print("\nWriting", output_image)
93  sitk.WriteImage(image, output_image)
94  written = True
95 else:
96  sys.exit(1)
97 
98 print()
itk::simple::ImageSeriesReader
Read series of image files into a SimpleITK image.
Definition: sitkImageSeriesReader.h:68
itk::simple::WriteImage
SITKIO_EXPORT void WriteImage(const Image &image, const std::vector< PathType > &fileNames, bool useCompression=false, int compressionLevel=-1)
WriteImage is a procedural interface to the ImageSeriesWriter. class which is convenient for many ima...