# SimpleITK: how to save dicom header into nii.gz file?

**URL:** <https://discourse.itk.org/t/simpleitk-how-to-save-dicom-header-into-nii-gz-file/4278>\
**Category:** Uncategorized\
**Created:** [July 18, 2021, 7:35am UTC](https://discourse.itk.org/t/simpleitk-how-to-save-dicom-header-into-nii-gz-file/4278 "2021-07-18T07:35:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhang-qiang-github](https://discourse.itk.org/user_avatar/discourse.itk.org/zhang-qiang-github/32/1183_2.png) [@zhang-qiang-github](https://discourse.itk.org/u/zhang-qiang-github)\
**Post date:** [July 18, 2021, 7:35am UTC](https://discourse.itk.org/t/simpleitk-how-to-save-dicom-header-into-nii-gz-file/4278/1 "2021-07-18T07:35:03Z")

</div>

I can save data into nii.gz file, but I find that the saved file do not include the dicom header information.

For example:

I load a `mha` as following:

```auto
reader = sitk.ImageFileReader()
reader.SetFileName("C:\\Users\\MLoong\\Desktop\\test_cas\\tof.mha")
reader.LoadPrivateTagsOn()
reader.ReadImageInformation()
position = reader.GetMetaData("0020|0032")
print(position)

```

And the printed information is what I want:

```auto
-107.45459007851\34.52157503367019\81.0415926597073

```

Then, I save the image into nii.gz file:

```auto
sitk_img=reader.Execute()
sitk.WriteImage(sitk_img, fileName="test.nii.gz")

```

And it succeed.

Then, I load this nii.gz file, and want to print `0020|0032` information:

```auto
reader = sitk.ImageFileReader()
reader.SetFileName("test.nii.gz")
reader.LoadPrivateTagsOn()
reader.ReadImageInformation()
position = reader.GetMetaData("0020|0032")
print(position)

```

A bug is reported:

```auto
Traceback (most recent call last):
  File "D:\Program Files\JetBrains\PyCharm Community Edition 2019.3.4\plugins\python-ce\helpers\pydev\pydevd.py", line 1434, in _exec
    pydev_imports.execfile(file, globals, locals) # execute the script
  File "D:\Program Files\JetBrains\PyCharm Community Edition 2019.3.4\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/MLoong/Desktop/medpro/Demo/test.py", line 19, in <module>
    position = reader.GetMetaData("0020|0032")
  File "D:\Anaconda3\envs\gitlab_medpro\lib\site-packages\SimpleITK\SimpleITK.py", line 5827, in GetMetaData
    return _SimpleITK.ImageFileReader_GetMetaData(self, key)
RuntimeError: Exception thrown in SimpleITK ImageFileReader_GetMetaData: D:\a\1\sitk-build\ITK\Modules\Core\Common\src\itkMetaDataDictionary.cxx:77:
itk::ERROR: Key '0020|0032' does not exist 

```

How can I save dicom header information into nii.gz file? Any suggestion is appreciated~~~

---

<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 18, 2021, 1:17pm UTC](https://discourse.itk.org/t/simpleitk-how-to-save-dicom-header-into-nii-gz-file/4278/2 "2021-07-18T13:17:40Z")

</div>

Hello @zhang-qiang-github,

Not sure here what you are trying to do. You refer to a DICOM tag (image-position-patient, 0020|0032) but code appears to read a file in another format (mha), not sure what is going on here.

Also when reading a set of 2D slices in DICOM format into a 3D volume, each of the slices has this tag, but once you have a volume and save it to a single file (nii.gz) which of the tags should you keep? In any case, this value is readily obtained from the image itself on a per slice basis:

```auto
# read nii.gz volume
i = 5 # slice number six
print(image.TransformIndexToPhysicalPoint([0,0,i]))

```

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [July 18, 2021, 1:52pm UTC](https://discourse.itk.org/t/simpleitk-how-to-save-dicom-header-into-nii-gz-file/4278/3 "2021-07-18T13:52:32Z")

</div>

Nifti format has a very limited and hardcoded metadata it supports. If you save to `.mha` or `.nrrd` you should be able to load the matadata you read from DICOM.
