I had to save images into dicoms. I did it with a help of example from docs. I wasn’t able to save modified metadata - most importantly uids.
To prove it I prepared simplest script generating zero-filled dicoms and it does not work either.
I am missing something?
import SimpleITK as sitk
import numpy as np
import os
import pydicom
os.makedirs("out", exist_ok=True)
slices_count = 5
study_uid = str(pydicom.uid.generate_uid())
series_uid = str(pydicom.uid.generate_uid())
patient_uid = str(pydicom.uid.generate_uid())
direction = (1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0)
#direction = niimg.GetDirection()
# Set the image's meta-information (DICOM tags).
# writer.KeepOriginalImageUIDOff()
# Use the study/series/frame of reference information given in the meta-data
# dictionary and not the automatically generated information from the file IO
writer = sitk.ImageFileWriter()
writer.KeepOriginalImageUIDOn()
for i in range(slices_count):
image_slice = sitk.GetImageFromArray(np.zeros((64,64), dtype=np.uint8))
image_slice.SetMetaData("(0020|0037)", '\\'.join(map(str, (direction[0], direction[3], direction[6],
direction[1],direction[4],direction[7]))))
image_slice.SetMetaData("(0020,000d)", study_uid)
image_slice.SetMetaData("(0020,000e)", series_uid)
image_slice.SetMetaData("(0010,0020)", patient_uid)
image_slice.SetMetaData("(0008,0060)", "CT")
# Instance Number
image_slice.SetMetaData("0020|0013", str(i))
image_slice.SetMetaData(
"0020|0032",
"\\".join(map(str, [0, 0, i])),
)
print([(k,image_slice.GetMetaData(k)) for k in image_slice.GetMetaDataKeys()])
# Write to the output directory and add the extension dcm, to force writing
# in DICOM format.
writer.SetFileName(os.path.join("out", str(i) + ".dcm"))
writer.Execute(image_slice)
Output:
[('(0008,0060)', 'CT'), ('(0010,0020)', '1.2.826.0.1.3680043.8.498.71806937837524007017059679289656591893'), ('(0020,000d)', '1.2.826.0.1.3680043.8.498.98560560569895736354598365450656533363'), ('(0020,000e)', '1.2.826.0.1.3680043.8.498.86398285000679321064707830263869202609'), ('(0020|0037)', '1.0\\0.0\\0.0\\0.0\\1.0\\0.0'), ('0020|0013', '0'), ('0020|0032', '0\\0\\0')]
[('(0008,0060)', 'CT'), ('(0010,0020)', '1.2.826.0.1.3680043.8.498.71806937837524007017059679289656591893'), ('(0020,000d)', '1.2.826.0.1.3680043.8.498.98560560569895736354598365450656533363'), ('(0020,000e)', '1.2.826.0.1.3680043.8.498.86398285000679321064707830263869202609'), ('(0020|0037)', '1.0\\0.0\\0.0\\0.0\\1.0\\0.0'), ('0020|0013', '1'), ('0020|0032', '0\\0\\1')]
[('(0008,0060)', 'CT'), ('(0010,0020)', '1.2.826.0.1.3680043.8.498.71806937837524007017059679289656591893'), ('(0020,000d)', '1.2.826.0.1.3680043.8.498.98560560569895736354598365450656533363'), ('(0020,000e)', '1.2.826.0.1.3680043.8.498.86398285000679321064707830263869202609'), ('(0020|0037)', '1.0\\0.0\\0.0\\0.0\\1.0\\0.0'), ('0020|0013', '2'), ('0020|0032', '0\\0\\2')]
[('(0008,0060)', 'CT'), ('(0010,0020)', '1.2.826.0.1.3680043.8.498.71806937837524007017059679289656591893'), ('(0020,000d)', '1.2.826.0.1.3680043.8.498.98560560569895736354598365450656533363'), ('(0020,000e)', '1.2.826.0.1.3680043.8.498.86398285000679321064707830263869202609'), ('(0020|0037)', '1.0\\0.0\\0.0\\0.0\\1.0\\0.0'), ('0020|0013', '3'), ('0020|0032', '0\\0\\3')]
[('(0008,0060)', 'CT'), ('(0010,0020)', '1.2.826.0.1.3680043.8.498.71806937837524007017059679289656591893'), ('(0020,000d)', '1.2.826.0.1.3680043.8.498.98560560569895736354598365450656533363'), ('(0020,000e)', '1.2.826.0.1.3680043.8.498.86398285000679321064707830263869202609'), ('(0020|0037)', '1.0\\0.0\\0.0\\0.0\\1.0\\0.0'), ('0020|0013', '4'), ('0020|0032', '0\\0\\4')]
% pydicom show out/4.dcm
Dataset.file_meta -------------------------------
(0002,0000) File Meta Information Group Length UL: 258
(0002,0001) File Meta Information Version OB: b'\x00\x01'
(0002,0002) Media Storage SOP Class UID UI: Secondary Capture Image Storage
(0002,0003) Media Storage SOP Instance UID UI: 1.2.826.0.1.3680043.2.1125.1.8912750519538242706577295536172681
(0002,0010) Transfer Syntax UID UI: Implicit VR Little Endian
(0002,0012) Implementation Class UID UI: 1.2.826.0.1.3680043.2.1143.107.104.103.115.3.0.24.111.124.113
(0002,0013) Implementation Version Name SH: 'GDCM 3.0.24'
(0002,0016) Source Application Entity Title AE: 'GDCM/ITK 5.4.0'
-------------------------------------------------
(0008,0016) SOP Class UID UI: Secondary Capture Image Storage
(0008,0018) SOP Instance UID UI: 1.2.826.0.1.3680043.2.1125.1.8912750519538242706577295536172681
(0008,0020) Study Date DA: '20241030'
(0008,0030) Study Time TM: '202112.896434'
(0008,0050) Accession Number SH: ''
(0008,0060) Modality CS: 'OT'
(0008,0064) Conversion Type CS: 'WSD'
(0008,0090) Referring Physician's Name PN: ''
(0010,0010) Patient's Name PN: ''
(0010,0020) Patient ID LO: ''
(0010,0030) Patient's Birth Date DA: ''
(0010,0040) Patient's Sex CS: ''
(0018,2010) Nominal Scanned Pixel Spacing DS: [1, 1]
(0020,000D) Study Instance UID UI: 1.2.826.0.1.3680043.2.1125.1.24150698580377258376551198836701802
(0020,000E) Series Instance UID UI: 1.2.826.0.1.3680043.2.1125.1.72909148603177487433987107850774838
(0020,0010) Study ID SH: ''
(0020,0011) Series Number IS: None
(0020,0013) Instance Number IS: '4'
(0020,0020) Patient Orientation CS: ''
(0020,0032) Image Position (Patient) DS: [0, 0, 4]
(0028,0002) Samples per Pixel US: 1
(0028,0004) Photometric Interpretation CS: 'MONOCHROME2'
(0028,0010) Rows US: 64
(0028,0011) Columns US: 64
(0028,0100) Bits Allocated US: 8
(0028,0101) Bits Stored US: 8
(0028,0102) High Bit US: 7
(0028,0103) Pixel Representation US: 0
(0028,1052) Rescale Intercept DS: '0'
(0028,1053) Rescale Slope DS: '1'
(0028,1054) Rescale Type LO: 'US'
(7FE0,0010) Pixel Data OW: Array of 4096 elements
Environment:
Darwin Kernel Version 23.4.0: Wed Feb 21 21:44:54 PST 2024; root:xnu-10063.101.15~2/RELEASE_ARM64_T6030 arm64
Python 3.11.9
itk==5.4.0
SimpleITK==2.4.0