However, some metadata are still not recorded, such as the spacing even when doing
img.SetSpacing(original_volume.GetSpacing())
or, for example, img.SetMetaData('0028|0030', '2.5\2.5')
NOTE:
Opening the saved DICOM file in 3D slicer I can see all the metadata I defined, but ITK SNAP cannot read some metadata (like pixel spacing) and if I use the validator on All CIODs – DICOM Standard Browser it also says that some metadata tags are missing
Setting the meta-data explicitly (i.e. img.SetMetaData('0028|0030', '2.5\2.5')) is ignored. ITK/SimpleITK will use the spacing from the actual image. The SetSpacing should do what you want. It does work for the images I’ve worked with, so not sure what is going on with your code.
ITK-SNAP does read the pixel spacing, it just doesn’t display it under the “Metadata” tab of the image information. You will find it under the “Info” tab.
With respect to the complaints about missing tags, the example is generic and the documentation states that it is up to the developer to create the relevant tags and set them correctly, comment starting with “# IMPORTANT: There are many DICOM tags that need to be updated when you modify…”
Generally speaking, saving a segmentation this way is a “hack”. The official way is to use DICOM-SEG. If you want to go that route, I’d recommend looking into using highdicom or dcmqi.