# Python writing is not working correctly

**URL:** <https://discourse.itk.org/t/python-writing-is-not-working-correctly/3864>\
**Category:** Engineering\
**Created:** [February 15, 2021, 3:15pm UTC](https://discourse.itk.org/t/python-writing-is-not-working-correctly/3864 "2021-02-15T15:15:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sarthakpati](https://discourse.itk.org/user_avatar/discourse.itk.org/sarthakpati/32/383_2.png) [@sarthakpati](https://discourse.itk.org/u/sarthakpati)\
**Post date:** [February 15, 2021, 3:15pm UTC](https://discourse.itk.org/t/python-writing-is-not-working-correctly/3864/1 "2021-02-15T15:15:42Z")

</div>

Hi,

The write image function in both SimpleITK and ITK python wrapping is broken.

I am having a very weird issue when writing images whose information has changed:

```python
>>> import SimpleITK as sitk
>>> base_image = sitk.ReadImage("C:/test/test_flair.nii.gz")
>>> test_image = sitk.ReadImage("C:/test/test_001.nii.gz")
>>> test_image.CopyInformation(base_image)
>>> sitk.WriteImage(test_image, "C:/test/test_new.nii.gz")
>>> test_image_new = sitk.ReadImage("C:/test/test_new.nii.gz")
>>> test_image_new.GetOrigin()
(0.0, 0.0, 0.0)
>>> test_image.GetOrigin()
(-0.0, -239.0, 0.0)

```

As you can see, the information is no longer preserved. I am running on Windows with SimpleITK version 1.2.4 and 2.0.2. Any help towards this would be very helpful, thank you. I am also able to replicate this in ITK version 5.0.2 using Python wrapping. This is replicable across Windows and Linux.

Here are the files:  
[test\_001.nii.gz](https://discourse.itk.org/uploads/short-url/z2fzIjIBGJOSbr97c5Q39PrO72k.gz) (69.6 KB) [test\_flair.nii.gz](https://discourse.itk.org/uploads/short-url/qfWr8d08BBBeuM1WvMjRKi9TeKK.gz) (2.2 MB) [test\_new.nii.gz](https://discourse.itk.org/uploads/short-url/kNfh4gzfC21aGul2fj723ct0ijH.gz) (28 KB)

Cross-post from [https://github.com/SimpleITK/SimpleITK/issues/1323](https://github.com/SimpleITK/SimpleITK/issues/1323)

Cheers,  
Sarthak

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [February 16, 2021, 2:35pm UTC](https://discourse.itk.org/t/python-writing-is-not-working-correctly/3864/2 "2021-02-16T14:35:05Z")

</div>

Hello,

The Nifti file format has a lot of complicated and redundant meta-data information. You can use the [Meta-Data Dictionary and Print](https://simpleitk.readthedocs.io/en/master/link_DicomImagePrintTags_docs.html) to inspect all the fields of the input images. There are likely inconsistencies caused by your approach of bulk copying all the meta data.

If you are not able to inspect the Nifti meta data, and determine what specific meta-data fields need to changed for your purpose, perhaps a simpler file format such and MetaIO or Nrrd can be used.

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [February 16, 2021, 3:11pm UTC](https://discourse.itk.org/t/python-writing-is-not-working-correctly/3864/3 "2021-02-16T15:11:17Z")

</div>

Hello @sarthakpati,

Based on the following MWE I suspect there is a bug in the underlying ITK implementation (copying the metadata dictionary will resolve your issue, but isn’t what is expected). When it isn’t copied the srow\_x, srow\_y, srow\_z values in the written nifti are all zeros:

```auto
import SimpleITK as sitk

base_image = sitk.ReadImage("test_flair.nii.gz")
test_image = sitk.ReadImage("test_001.nii.gz")
test_image.CopyInformation(base_image)
sitk.WriteImage(test_image, "test_new.nii.gz")
test_image_new = sitk.ReadImage("test_new.nii.gz")
print('Expected path:')
print(test_image.GetOrigin())
print(test_image_new.GetOrigin())

base_image = sitk.ReadImage("test_flair.nii.gz")
test_image = sitk.ReadImage("test_001.nii.gz")
test_image.CopyInformation(base_image)
for k in base_image.GetMetaDataKeys():
    test_image.SetMetaData(k, base_image.GetMetaData(k))
sitk.WriteImage(test_image, "test_new.nii.gz")
test_image_new = sitk.ReadImage("test_new.nii.gz")
print('Unexpected path:')
print(test_image.GetOrigin())
print(test_image_new.GetOrigin())

```

@blowekamp, @hjmjohnson, @dzenanz, any idea what is going on (I’m “at” a conference so didn’t have too much time to dig into this).

---

<div class="post-metadata">

**Author:** ![sarthakpati](https://discourse.itk.org/user_avatar/discourse.itk.org/sarthakpati/32/383_2.png) [@sarthakpati](https://discourse.itk.org/u/sarthakpati)\
**Post date:** [February 16, 2021, 3:18pm UTC](https://discourse.itk.org/t/python-writing-is-not-working-correctly/3864/4 "2021-02-16T15:18:37Z")

</div>

Update from [GitHub](https://github.com/SimpleITK/SimpleITK/issues/1323#issuecomment-779903039):

This issue is not present in [itk 5.2rc2](https://pypi.org/project/itk/5.2rc2/)

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [February 16, 2021, 3:57pm UTC](https://discourse.itk.org/t/python-writing-is-not-working-correctly/3864/5 "2021-02-16T15:57:54Z")

</div>

I’m not sure which commit in ITK fixed the issue. Can you check the [SimpleITK “latest”](https://simpleitk.readthedocs.io/en/master/gettingStarted.html#latest-binaries) binary which is build against ITK from Jan 28th to see if it contains this issue. I am working on a pack to update SimpleITK super build the rc2.

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [February 16, 2021, 4:15pm UTC](https://discourse.itk.org/t/python-writing-is-not-working-correctly/3864/6 "2021-02-16T16:15:00Z")

</div>

Just checked with the latest SimpleITK, that version resolved the issue.

Best usage of my time while listening to an awards ceremony 😉 .
