1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19""" A SimpleITK example demonstrating how to write a DICOM series. """
20
21import sys
22import time
23import os
24import numpy as np
25import SimpleITK as sitk
26
27pixel_dtypes = {"int16": np.int16, "float64": np.float64}
28
29
30def writeSlices(series_tag, in_image, out_dir, i):
31 """ Write slices to output directory """
32 image_slice = in_image[:, :, i]
33
34
35 list(
36 map(
37 lambda tag_value: image_slice.SetMetaData(tag_value[0], tag_value[1]),
38 series_tag,
39 )
40 )
41
42
43
44 image_slice.SetMetaData("0008|0012", time.strftime("%Y%m%d"))
45
46 image_slice.SetMetaData("0008|0013", time.strftime("%H%M%S"))
47
48
49
50 image_slice.SetMetaData("0008|0060", "CT")
51
52
53
54
55 image_slice.SetMetaData(
56 "0020|0032",
57 "\\".join(map(str, in_image.TransformIndexToPhysicalPoint((0, 0, i)))),
58 )
59
60 image_slice.SetMetaData("0020|0013", str(i))
61
62
63
64 writer.SetFileName(os.path.join(out_dir, str(i) + ".dcm"))
65 writer.Execute(image_slice)
66
67
68if len(sys.argv) < 3:
69 print(
70 "Usage: python "
71 + __file__
72 + " <output_directory> ["
73 + ", ".join(pixel_dtypes)
74 + "]"
75 )
76 sys.exit(1)
77
78
79try:
80 pixel_dtype = pixel_dtypes[sys.argv[2]]
81except KeyError:
82 pixel_dtype = pixel_dtypes["int16"]
83
84new_arr = np.random.uniform(-10, 10, size=(3, 4, 5)).astype(pixel_dtype)
85new_img = sitk.GetImageFromArray(new_arr)
86new_img.SetSpacing([2.5, 3.5, 4.5])
87
88
89
90
91
92
93
94
95
96
97
98
100
101
102writer.KeepOriginalImageUIDOn()
103
104modification_time = time.strftime("%H%M%S")
105modification_date = time.strftime("%Y%m%d")
106
107
108
109
110
111direction = new_img.GetDirection()
112series_tag_values = [
113 ("0008|0031", modification_time),
114 ("0008|0021", modification_date),
115 ("0008|0008", "DERIVED\\SECONDARY"),
116 (
117 "0020|000e",
118 "1.2.826.0.1.3680043.2.1125." + modification_date + ".1" + modification_time,
119 ),
120 (
121 "0020|0037",
122 "\\".join(
123 map(
124 str,
125 (
126 direction[0],
127 direction[3],
128 direction[6],
129 direction[1],
130 direction[4],
131 direction[7],
132 ),
133 )
134 ),
135 ),
136
137 ("0008|103e", "Created-SimpleITK"),
138]
139
140if pixel_dtype == np.float64:
141
142
143
144
145 rescale_slope = 0.001
146 series_tag_values = series_tag_values + [
147 ("0028|1053", str(rescale_slope)),
148 ("0028|1052", "0"),
149 ("0028|0100", "16"),
150 ("0028|0101", "16"),
151 ("0028|0102", "15"),
152 ("0028|0103", "1"),
153 ]
154
155
156list(
157 map(
158 lambda i: writeSlices(series_tag_values, new_img, sys.argv[1], i),
159 range(new_img.GetDepth()),
160 )
161)
162
163
164
165
166data_directory = sys.argv[1]
167series_IDs = sitk.ImageSeriesReader.GetGDCMSeriesIDs(data_directory)
168if not series_IDs:
169 print(
170 'ERROR: given directory "'
171 + data_directory
172 + '" does not contain a DICOM series.'
173 )
174 sys.exit(1)
175series_file_names = sitk.ImageSeriesReader.GetGDCMSeriesFileNames(
176 data_directory, series_IDs[0]
177)
178
180series_reader.SetFileNames(series_file_names)
181
182
183
184
185
186
187series_reader.LoadPrivateTagsOn()
188image3D = series_reader.Execute()
189print(image3D.GetSpacing(), "vs", new_img.GetSpacing())
190sys.exit(0)
Write out a SimpleITK image to the specified file location.
Read series of image files into a SimpleITK image.