19 import SimpleITK
as sitk
26 pixel_dtypes = {
"int16": np.int16,
27 "float64": np.float64}
30 def writeSlices(series_tag_values, new_img, out_dir, i):
31 image_slice = new_img[:, :, i]
34 list(map(
lambda tag_value: image_slice.SetMetaData(tag_value[0],
40 image_slice.SetMetaData(
"0008|0012", time.strftime(
"%Y%m%d"))
42 image_slice.SetMetaData(
"0008|0013", time.strftime(
"%H%M%S"))
46 image_slice.SetMetaData(
"0008|0060",
"CT")
51 image_slice.SetMetaData(
"0020|0032",
'\\'.join(
52 map(str, new_img.TransformIndexToPhysicalPoint((0, 0, i)))))
54 image_slice.SetMetaData(
"0020,0013", str(i))
58 writer.SetFileName(os.path.join(out_dir, str(i) +
'.dcm'))
59 writer.Execute(image_slice)
63 print(
"Usage: python " + __file__ +
" <output_directory> [" +
", "
64 .join(pixel_dtypes) +
"]")
69 pixel_dtype = pixel_dtypes[sys.argv[2]]
71 pixel_dtype = pixel_dtypes[
"int16"]
73 new_arr = np.random.uniform(-10, 10, size=(3, 4, 5)).astype(pixel_dtype)
74 new_img = sitk.GetImageFromArray(new_arr)
75 new_img.SetSpacing([2.5, 3.5, 4.5])
91 writer.KeepOriginalImageUIDOn()
93 modification_time = time.strftime(
"%H%M%S")
94 modification_date = time.strftime(
"%Y%m%d")
100 direction = new_img.GetDirection()
101 series_tag_values = [
102 (
"0008|0031", modification_time),
103 (
"0008|0021", modification_date),
104 (
"0008|0008",
"DERIVED\\SECONDARY"),
105 (
"0020|000e",
"1.2.826.0.1.3680043.2.1125."
106 + modification_date +
".1" + modification_time),
107 (
"0020|0037",
'\\'.join(map(str, (direction[0], direction[3], direction[6],
108 direction[1], direction[4],
111 (
"0008|103e",
"Created-SimpleITK")
114 if pixel_dtype == np.float64:
119 rescale_slope = 0.001
120 series_tag_values = series_tag_values + [
121 (
'0028|1053', str(rescale_slope)),
129 list(map(
lambda i: writeSlices(series_tag_values, new_img, sys.argv[1], i),
130 range(new_img.GetDepth())))
135 data_directory = sys.argv[1]
136 series_IDs = sitk.ImageSeriesReader.GetGDCMSeriesIDs(data_directory)
138 print(
"ERROR: given directory \"" + data_directory +
139 "\" does not contain a DICOM series.")
141 series_file_names = sitk.ImageSeriesReader.GetGDCMSeriesFileNames(
142 data_directory, series_IDs[0])
145 series_reader.SetFileNames(series_file_names)
152 series_reader.LoadPrivateTagsOn()
153 image3D = series_reader.Execute()
154 print(image3D.GetSpacing(),
'vs', new_img.GetSpacing())