19 from __future__
import print_function
21 import SimpleITK
as sitk
25 if len( sys.argv ) < 3:
26 print(
"Usage: python " + __file__ +
" <input_directory_with_DICOM_series> <output_directory>" )
32 data_directory = sys.argv[1]
33 series_IDs = sitk.ImageSeriesReader.GetGDCMSeriesIDs(data_directory)
35 print(
"ERROR: given directory \""+data_directory+
"\" does not contain a DICOM series.")
37 series_file_names = sitk.ImageSeriesReader.GetGDCMSeriesFileNames(data_directory, series_IDs[0])
40 series_reader.SetFileNames(series_file_names)
47 series_reader.MetaDataDictionaryArrayUpdateOn()
48 series_reader.LoadPrivateTagsOn()
49 image3D = series_reader.Execute()
64 writer.KeepOriginalImageUIDOn()
68 tags_to_copy = [
"0010|0010",
79 modification_time = time.strftime(
"%H%M%S")
80 modification_date = time.strftime(
"%Y%m%d")
86 direction = filtered_image.GetDirection()
87 series_tag_values = [(k, series_reader.GetMetaData(0,k))
for k
in tags_to_copy
if series_reader.HasMetaDataKey(0,k)] + \
88 [(
"0008|0031",modification_time),
89 (
"0008|0021",modification_date),
90 (
"0008|0008",
"DERIVED\\SECONDARY"),
91 (
"0020|000e",
"1.2.826.0.1.3680043.2.1125."+modification_date+
".1"+modification_time),
92 (
"0020|0037",
'\\'.join(map(str, (direction[0], direction[3], direction[6],
93 direction[1],direction[4],direction[7])))),
94 (
"0008|103e", series_reader.GetMetaData(0,
"0008|103e") +
" Processed-SimpleITK")]
96 for i
in range(filtered_image.GetDepth()):
97 image_slice = filtered_image[:,:,i]
99 for tag, value
in series_tag_values:
100 image_slice.SetMetaData(tag, value)
102 image_slice.SetMetaData(
"0008|0012", time.strftime(
"%Y%m%d"))
103 image_slice.SetMetaData(
"0008|0013", time.strftime(
"%H%M%S"))
104 image_slice.SetMetaData(
"0020|0032",
'\\'.join(map(str,filtered_image.TransformIndexToPhysicalPoint((0,0,i)))))
105 image_slice.SetMetaData(
"0020|0013", str(i))
108 writer.SetFileName(os.path.join(sys.argv[2],str(i)+
'.dcm'))
109 writer.Execute(image_slice)