# I am trying to copy all the metadata/header information from one image to another.

**URL:** <https://discourse.itk.org/t/i-am-trying-to-copy-all-the-metadata-header-information-from-one-image-to-another/6120>\
**Category:** Algorithms\
**Tags:** simpleitk, python\
**Created:** [July 31, 2023, 2:31pm UTC](https://discourse.itk.org/t/i-am-trying-to-copy-all-the-metadata-header-information-from-one-image-to-another/6120 "2023-07-31T14:31:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cristobal\_Rodero](https://discourse.itk.org/user_avatar/discourse.itk.org/cristobal_rodero/32/3615_2.png) [@Cristobal\_Rodero](https://discourse.itk.org/u/Cristobal_Rodero)\
**Post date:** [July 31, 2023, 2:31pm UTC](https://discourse.itk.org/t/i-am-trying-to-copy-all-the-metadata-header-information-from-one-image-to-another/6120/1 "2023-07-31T14:31:00Z")

</div>

I have a segmentation (original\_image) that when I overlap it with the MRI image it comes from it overlaps as expected (comparing it in 3D Slicer, for instance). I perform some operations on Seg3D (or any other software that saves just the image array) and when I save the image (new\_image) the overlap doesn’t occur anymore, it’s moved and rotated. I have the function below using SimpleITK (sitk) to try to copy all the possible metadata/header/information from the original\_image but it still doesn’t align. The center seems to be fine, but that’s it. I understand that there are probably redundancies in the script, I was trying all possible options.

```auto
def save_itk_keeping_header(new_image, original_image, filename):

  image_bad_header_itk=sitk.ReadImage(new_image)
  image_good_header=sitk.ReadImage(original_image)

  image_bad_header_itk.CopyInformation(image_good_header)

  image_bad_header_itk.SetDirection(image_good_header.GetDirection())
  image_bad_header_itk.SetOrigin(image_good_header.GetOrigin())
  [image_bad_header_itk.SetMetaData(key,image_good_header.GetMetaData(key)) for key in image_good_header.GetMetaDataKeys()]

  sitk.WriteImage(image_bad_header_itk, filename, True)

```

The output I get is below - it seems that it should be rotated 90 degrees. Any idea what am I missing? In case it helps, the grey image is an MRI, so they are usually rotated in contrast with CT images that are align with the XYZ coordinate system. Also, I know I’m not copying the spacing - it’s one of the modifications I do - but even if I add it, that doesn’t solve my problem.

![TjWuC](https://discourse.itk.org/uploads/default/original/2X/5/561382234da30df85b25a613974123798b7d25a2.png)

---

<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:** [July 31, 2023, 3:17pm UTC](https://discourse.itk.org/t/i-am-trying-to-copy-all-the-metadata-header-information-from-one-image-to-another/6120/2 "2023-07-31T15:17:34Z")

</div>

Hello @Cristobal_Rodero,

Just guessing here, but possibly the [coordinate systems](https://slicer.readthedocs.io/en/latest/user_guide/coordinate_systems.html) were corrupted when you did your work. Try forcing to the standard ITK configuration:

```auto
image_bad_header_itk=sitk.ReadImage(new_image)
image_good_header = sitk.DICOMOrient(image_bad_header_itk, "LPS")

```

If this doesn’t work, you will need to share data so that we can reproduce and try to help you (anonymize before sharing or even better, share a small artificial example).

---

<div class="post-metadata">

**Author:** ![Cristobal\_Rodero](https://discourse.itk.org/user_avatar/discourse.itk.org/cristobal_rodero/32/3615_2.png) [@Cristobal\_Rodero](https://discourse.itk.org/u/Cristobal_Rodero)\
**Post date:** [August 29, 2023, 7:29am UTC](https://discourse.itk.org/t/i-am-trying-to-copy-all-the-metadata-header-information-from-one-image-to-another/6120/3 "2023-08-29T07:29:24Z")

</div>

Hello @zivy,  
Apologies for the late reply. I’ve been doing some more tests and I confirm that this works every time. thank you so much!
