1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22""" This example shows how to read a specific series from a Dicom directory that
23may contain more than one series. The script scans for all series. If an
24output name is given, it writes out the requested series. If no specific
25series name is given, the first series found is written.
26"""
27
28
29import sys
30import getopt
31import SimpleITK as sitk
32
33target_series = ""
34output_image = ""
35
36
37def 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
45try:
46 opts, args = getopt.getopt(sys.argv[1:], "s:", ["series"])
47except getopt.GetoptError:
48 usage()
49 sys.exit(1)
50
51for o, a in opts:
52 if o in ("-s", "--series"):
53 target_series = a
54 else:
55 assert False, "unhandled options"
56
57
58if len(args) < 1:
59 print(args)
60 usage()
61 sys.exit(1)
62
63input_directory = args[0]
64if len(args) > 1:
65 output_image = args[1]
66
67
69written = False
70
71series_found = reader.GetGDCMSeriesIDs(input_directory)
72
73
74if len(series_found):
75
76 for serie in series_found:
77
78 print("\nSeries:", serie)
79
80
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)
94 written = True
95else:
96 sys.exit(1)
97
98print()
Read series of image files into a SimpleITK image.
SITKIO_EXPORT void WriteImage(const Image &image, const PathType &fileName, bool useCompression=false, int compressionLevel=-1)
WriteImage is a procedural interface to the ImageFileWriter. class which is convenient for many image...