SimpleITK ImageFileWriter give a wrong tag

I have some process for a image, and I want to save the result.

The complete code to save image is:

                writer = sitk.ImageFileWriter()
                writer.KeepOriginalImageUIDOn()
                writer.SetFileName(name)
                print(img.GetMetaData('0028|0030'))
                writer.Execute(img)
                tImg = sitk.ReadImage(name)
                print(tImg.GetMetaData('0028|0030'))

The tag of 0028|0030 is pixel spacing.

In the above code, I want to save img, and the printed information is:

0.34090909361839\0.34090909361839 
1\1 

The spacing of img is 0.34\0.34. But, when I write it and read it, the spacing is changed to be 1\1.

My enviornment is:

win 10
python 3.7
SimpleITK 2.0.2

Is there anything wrong with sitk.ImageFileWrite()?

Hello @zhang-qiang-github,

Please check the real pixel spacing for the image:

print(img.GetSpacing())

Likely this became 1,1 due to some processing done earlier in the code. This overrides the information found in the metadata dictionary when writing.

2 Likes

Thank you for your suggestion~~~

I use img.SetSpacing before writer.Execute, and it works. The result is correct.

1 Like