19 from __future__
import print_function
21 import SimpleITK
as sitk
26 if len( sys.argv ) < 2:
27 print(
"Usage: python " + __file__ +
"<output_directory>" )
30 def writeSlices(series_tag_values, new_img, i):
31 image_slice = new_img[:,:,i]
34 list(map(
lambda tag_value: image_slice.SetMetaData(tag_value[0], tag_value[1]), series_tag_values))
37 image_slice.SetMetaData(
"0008|0012", time.strftime(
"%Y%m%d"))
38 image_slice.SetMetaData(
"0008|0013", time.strftime(
"%H%M%S"))
41 image_slice.SetMetaData(
"0008|0060",
"CT")
44 image_slice.SetMetaData(
"0020|0032",
'\\'.join(map(str,new_img.TransformIndexToPhysicalPoint((0,0,i)))))
45 image_slice.SetMetaData(
"0020,0013", str(i))
48 writer.SetFileName(os.path.join(sys.argv[1],str(i)+
'.dcm'))
49 writer.Execute(image_slice)
52 new_arr = np.random.uniform(-10, 10, size = (3,4,5)).astype(np.int16)
53 new_img = sitk.GetImageFromArray(new_arr)
54 new_img.SetSpacing([2.5,3.5,4.5])
69 writer.KeepOriginalImageUIDOn()
71 modification_time = time.strftime(
"%H%M%S")
72 modification_date = time.strftime(
"%Y%m%d")
78 direction = new_img.GetDirection()
79 series_tag_values = [(
"0008|0031",modification_time),
80 (
"0008|0021",modification_date),
81 (
"0008|0008",
"DERIVED\\SECONDARY"),
82 (
"0020|000e",
"1.2.826.0.1.3680043.2.1125."+modification_date+
".1"+modification_time),
83 (
"0020|0037",
'\\'.join(map(str, (direction[0], direction[3], direction[6],
84 direction[1],direction[4],direction[7])))),
85 (
"0008|103e",
"Created-SimpleITK")]
88 list(map(
lambda i: writeSlices(series_tag_values, new_img, i), range(new_img.GetDepth())))
93 data_directory = sys.argv[1]
94 series_IDs = sitk.ImageSeriesReader.GetGDCMSeriesIDs(data_directory)
96 print(
"ERROR: given directory \""+data_directory+
"\" does not contain a DICOM series.")
98 series_file_names = sitk.ImageSeriesReader.GetGDCMSeriesFileNames(data_directory, series_IDs[0])
101 series_reader.SetFileNames(series_file_names)
108 series_reader.LoadPrivateTagsOn()
109 image3D = series_reader.Execute()
110 print(image3D.GetSpacing(),
'vs',new_img.GetSpacing())