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 #
23 # This example shows how to read a specific series from a Dicom directory that
24 # may contain more than one series. The script scans for all series. If an
25 # output name is given, it writes out the requested series. If no specific
26 # series name is given, the first series found is written.
27 #
28 
29 
30 import sys
31 import getopt
32 import SimpleITK as sitk
33 
34 target_series = ""
35 output_image = ""
36 
37 
38 def usage():
39  print(
40  "\nUsage: %s [-s series_name] input_directory [output_image]\n"
41  % (sys.argv[0])
42  )
43 
44 
45 # Parse command line options
46 try:
47  opts, args = getopt.getopt(sys.argv[1:], "s:", ["series"])
48 except getopt.GetoptError:
49  usage()
50  sys.exit(1)
51 
52 for o, a in opts:
53  if o in ("-s", "--series"):
54  target_series = a
55  else:
56  assert False, "unhandled options"
57 
58 # Get input/output names
59 if len(args) < 1:
60  print(args)
61  usage()
62  sys.exit(1)
63 
64 input_directory = args[0]
65 if len(args) > 1:
66  output_image = args[1]
67 
68 # Find the Dicom series
69 reader = sitk.ImageSeriesReader()
70 written = False
71 
72 series_found = reader.GetGDCMSeriesIDs(input_directory)
73 
74 # Process each Dicom series
75 if len(series_found):
76 
77  for serie in series_found:
78 
79  print("\nSeries:", serie)
80 
81  # Get the Dicom filename corresponding to the current series
82  dicom_names = reader.GetGDCMSeriesFileNames(input_directory, serie)
83 
84  print("\nFiles in series: ", dicom_names)
85 
86  if len(dicom_names):
87  reader.SetFileNames(dicom_names)
88  image = reader.Execute()
89  print("\nImage size: ", image.GetSize())
90 
91  if (output_image != "") and not written:
92  if target_series == "" or target_series == serie:
93  print("\nWriting", output_image)
94  sitk.WriteImage(image, output_image)
95  written = True
96 else:
97  sys.exit(1)
98 
99 print()
itk::simple::WriteImage
SITKIO_EXPORT void WriteImage(const Image &image, const std::vector< std::string > &fileNames, bool useCompression=false, int compressionLevel=-1)
WriteImage is a procedural interface to the ImageSeriesWriter. class which is convenient for many ima...
itk::simple::ImageSeriesReader
Read series of image files into a SimpleITK image.
Definition: sitkImageSeriesReader.h:55