19 """ A SimpleITK example script demonstrating how to read a DICOM series,
20 modify it, and then write a new DICOM series. """
25 import SimpleITK
as sitk
31 +
" <input_directory_with_DICOM_series> <output_directory>"
37 data_directory = sys.argv[1]
38 series_IDs = sitk.ImageSeriesReader.GetGDCMSeriesIDs(data_directory)
41 'ERROR: given directory "'
43 +
'" does not contain a DICOM series.'
46 series_file_names = sitk.ImageSeriesReader.GetGDCMSeriesFileNames(
47 data_directory, series_IDs[0]
51 series_reader.SetFileNames(series_file_names)
58 series_reader.MetaDataDictionaryArrayUpdateOn()
59 series_reader.LoadPrivateTagsOn()
60 image3D = series_reader.Execute()
75 writer.KeepOriginalImageUIDOn()
91 modification_time = time.strftime(
"%H%M%S")
92 modification_date = time.strftime(
"%Y%m%d")
105 direction = filtered_image.GetDirection()
106 series_tag_values = [
107 (k, series_reader.GetMetaData(0, k))
108 for k
in tags_to_copy
109 if series_reader.HasMetaDataKey(0, k)
111 (
"0008|0031", modification_time),
112 (
"0008|0021", modification_date),
113 (
"0008|0008",
"DERIVED\\SECONDARY"),
116 "1.2.826.0.1.3680043.2.1125." + modification_date +
".1" + modification_time,
138 series_reader.GetMetaData(0,
"0008|103e")
139 if series_reader.HasMetaDataKey(0,
"0008|103e")
140 else "" +
" Processed-SimpleITK"
145 for i
in range(filtered_image.GetDepth()):
146 image_slice = filtered_image[:, :, i]
148 for tag, value
in series_tag_values:
149 image_slice.SetMetaData(tag, value)
152 image_slice.SetMetaData(
"0008|0012", time.strftime(
"%Y%m%d"))
154 image_slice.SetMetaData(
"0008|0013", time.strftime(
"%H%M%S"))
156 image_slice.SetMetaData(
158 "\\".join(map(str, filtered_image.TransformIndexToPhysicalPoint((0, 0, i)))),
161 image_slice.SetMetaData(
"0020|0013", str(i))
165 writer.SetFileName(os.path.join(sys.argv[2], str(i) +
".dcm"))
166 writer.Execute(image_slice)