19 import SimpleITK
as sitk
26 pixel_dtypes = {
"int16": np.int16,
"float64": np.float64}
29 def writeSlices(series_tag_values, new_img, out_dir, i):
30 image_slice = new_img[:, :, i]
35 lambda tag_value: image_slice.SetMetaData(
36 tag_value[0], tag_value[1]
44 image_slice.SetMetaData(
"0008|0012", time.strftime(
"%Y%m%d"))
46 image_slice.SetMetaData(
"0008|0013", time.strftime(
"%H%M%S"))
50 image_slice.SetMetaData(
"0008|0060",
"CT")
55 image_slice.SetMetaData(
57 "\\".join(map(str, new_img.TransformIndexToPhysicalPoint((0, 0, i)))),
60 image_slice.SetMetaData(
"0020,0013", str(i))
64 writer.SetFileName(os.path.join(out_dir, str(i) +
".dcm"))
65 writer.Execute(image_slice)
72 +
" <output_directory> ["
73 +
", ".join(pixel_dtypes)
80 pixel_dtype = pixel_dtypes[sys.argv[2]]
82 pixel_dtype = pixel_dtypes[
"int16"]
84 new_arr = np.random.uniform(-10, 10, size=(3, 4, 5)).astype(pixel_dtype)
85 new_img = sitk.GetImageFromArray(new_arr)
86 new_img.SetSpacing([2.5, 3.5, 4.5])
102 writer.KeepOriginalImageUIDOn()
104 modification_time = time.strftime(
"%H%M%S")
105 modification_date = time.strftime(
"%Y%m%d")
111 direction = new_img.GetDirection()
112 series_tag_values = [
113 (
"0008|0031", modification_time),
114 (
"0008|0021", modification_date),
115 (
"0008|0008",
"DERIVED\\SECONDARY"),
118 "1.2.826.0.1.3680043.2.1125."
140 (
"0008|103e",
"Created-SimpleITK"),
143 if pixel_dtype == np.float64:
148 rescale_slope = 0.001
149 series_tag_values = series_tag_values + [
150 (
"0028|1053", str(rescale_slope)),
161 lambda i: writeSlices(series_tag_values, new_img, sys.argv[1], i),
162 range(new_img.GetDepth()),
169 data_directory = sys.argv[1]
170 series_IDs = sitk.ImageSeriesReader.GetGDCMSeriesIDs(data_directory)
173 'ERROR: given directory "'
175 +
'" does not contain a DICOM series.'
178 series_file_names = sitk.ImageSeriesReader.GetGDCMSeriesFileNames(
179 data_directory, series_IDs[0]
183 series_reader.SetFileNames(series_file_names)
190 series_reader.LoadPrivateTagsOn()
191 image3D = series_reader.Execute()
192 print(image3D.GetSpacing(),
"vs", new_img.GetSpacing())