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])
42 series_reader.SetFileNames(series_file_names)
43 image3D = series_reader.Execute()
50 image_reader.LoadPrivateTagsOn()
52 for file_name
in series_file_names:
53 image_reader.SetFileName(file_name)
54 img = image_reader.Execute()
55 image_list.append(img)
72 writer.KeepOriginalImageUIDOn()
73 modification_time = time.strftime(
"%H%M%S")
74 modification_date = time.strftime(
"%Y%m%d")
75 for i
in range(filtered_image.GetDepth()):
76 image_slice = filtered_image[:,:,i]
77 original_slice = image_list[i]
79 for k
in original_slice.GetMetaDataKeys():
80 if k!=
"0028|1052" and k!=
"0028|1053":
81 image_slice.SetMetaData(k, original_slice.GetMetaData(k))
83 image_slice.SetMetaData(
"0008|0031", modification_time)
84 image_slice.SetMetaData(
"0008|0021", modification_date)
85 image_slice.SetMetaData(
"0008|0008",
"DERIVED\SECONDARY")
88 image_slice.SetMetaData(
"0020|000e",
"1.2.826.0.1.3680043.2.1125."+modification_date+
".1"+modification_time)
90 writer.SetFileName(os.path.join(sys.argv[2], os.path.basename(series_file_names[i])) + (
'' if os.path.splitext(series_file_names[i])[1] ==
'.dcm' else '.dcm'))
91 writer.Execute(image_slice)