MCPcopy Create free account
hub / github.com/SimpleITK/SimpleITK / writeSlices

Function writeSlices

Examples/DicomSeriesFromArray/DicomSeriesFromArray.py:30–65  ·  view source on GitHub ↗

Write slices to output directory

(series_tag, in_image, out_dir, i)

Source from the content-addressed store, hash-verified

28
29
30def writeSlices(series_tag, in_image, out_dir, i):
31 """ Write slices to output directory """
32 image_slice = in_image[:, :, i]
33
34 # Tags shared by the series.
35 list(
36 map(
37 lambda tag_value: image_slice.SetMetaData(tag_value[0], tag_value[1]),
38 series_tag,
39 )
40 )
41
42 # Slice specific tags.
43 # Instance Creation Date
44 image_slice.SetMetaData("0008|0012", time.strftime("%Y%m%d"))
45 # Instance Creation Time
46 image_slice.SetMetaData("0008|0013", time.strftime("%H%M%S"))
47
48 # Setting the type to CT so that the slice location is preserved and
49 # the thickness is carried over.
50 image_slice.SetMetaData("0008|0060", "CT")
51
52 # (0020, 0032) image position patient determines the 3D spacing between
53 # slices.
54 # Image Position (Patient)
55 image_slice.SetMetaData(
56 "0020|0032",
57 "\\".join(map(str, in_image.TransformIndexToPhysicalPoint((0, 0, i)))),
58 )
59 # Instance Number
60 image_slice.SetMetaData("0020|0013", str(i))
61
62 # Write to the output directory and add the extension dcm, to force
63 # writing in DICOM format.
64 writer.SetFileName(os.path.join(out_dir, str(i) + ".dcm"))
65 writer.Execute(image_slice)
66
67
68if len(sys.argv) < 3:

Callers 1

Calls 4

SetMetaDataMethod · 0.80
SetFileNameMethod · 0.45
ExecuteMethod · 0.45

Tested by

no test coverage detected