# vtkMetaImageReader: getting additional (arbitrary) meta tags?

**URL:** <https://discourse.vtk.org/t/vtkmetaimagereader-getting-additional-arbitrary-meta-tags/10728>\
**Category:** Development\
**Tags:** python, file-formats\
**Created:** [February 19, 2023, 9:07pm UTC](https://discourse.vtk.org/t/vtkmetaimagereader-getting-additional-arbitrary-meta-tags/10728 "2023-02-19T21:07:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ingmar](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/i/cc9497/32.png) [@Ingmar](https://discourse.vtk.org/u/Ingmar)\
**Post date:** [February 19, 2023, 9:07pm UTC](https://discourse.vtk.org/t/vtkmetaimagereader-getting-additional-arbitrary-meta-tags/10728/1 "2023-02-19T21:07:42Z")

</div>

MetaIO image files may contain various tags (such as [https://itk.org/Wiki/ITK/MetaIO/Documentation#Associated\_transformations](https://itk.org/Wiki/ITK/MetaIO/Documentation#Associated_transformations) and [https://itk.org/Wiki/ITK/MetaIO/Documentation#Reference:\_Tags\_of\_MetaImage](https://itk.org/Wiki/ITK/MetaIO/Documentation#Reference:_Tags_of_MetaImage)) such as

```auto
CenterOfRotation = 20 30 40
AnatomicalOrientation = RAI

```

and even let the user add their own arbitrary information, such as

```auto
IntensityRange = 0 4095
InterceptSlope = -1024 1

```

However from the documentation (e.g. [https://vtk.org/doc/nightly/html/classvtkMetaImageReader.html](https://vtk.org/doc/nightly/html/classvtkMetaImageReader.html) and [https://vtk.org/doc/nightly/html/classvtkImageData.html](https://vtk.org/doc/nightly/html/classvtkImageData.html)) I do not actually see a way to access this information, and when saving with vtkMetaImageWriter, none of that actually seems to actually propagate to the output file, which then e.g. looks like

```auto
CenterOfRotation = 0 0 0
AnatomicalOrientation = ???

```

and other tags mentioned above (`IntensityRange` and `InterceptSlope`) do not even exist in the output.

Is there a actually way, how I can address that (preferably in python)? I did see `vtkMetaImageReader.GetInformation()` but was not quite sure how to use it, [https://vtk.org/doc/nightly/html/classvtkInformation.html](https://vtk.org/doc/nightly/html/classvtkInformation.html) did not describe how to actually even get key names etc.

Please let me know

---

<div class="post-metadata">

**Author:** ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)\
**Post date:** [February 20, 2023, 12:11am UTC](https://discourse.vtk.org/t/vtkmetaimagereader-getting-additional-arbitrary-meta-tags/10728/2 "2023-02-20T00:11:41Z")

</div>

The GetInformation() method doesn’t give you the information you want, it returns information related to pipeline execution and is unrelated to the file header. The vtkMetaImageReader does not, in fact, read those tags. I don’t think anyone has any plans to add them.

But the meta header is just text, and easy to read in Python. This should get you started, though you’ll have to handle the string-to-number conversions:

```python
meta = {} # dictionary to store the header in

with open(filename) as f:
    for line in f.readlines():
        tag, values = line.split('=', 1)
        tag = tag.strip()
        values = [v.strip() for v in values.split()]
        meta[tag] = values
        if tag == "ElementDataFile":
            break

```

---

<div class="post-metadata">

**Author:** ![Ingmar](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/i/cc9497/32.png) [@Ingmar](https://discourse.vtk.org/u/Ingmar)\
**Post date:** [February 20, 2023, 6:33am UTC](https://discourse.vtk.org/t/vtkmetaimagereader-getting-additional-arbitrary-meta-tags/10728/3 "2023-02-20T06:33:11Z")

</div>

I see - thanks @dgobbi ! Then I would have to do the same when writing back to disk, right?

---

<div class="post-metadata">

**Author:** ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)\
**Post date:** [February 20, 2023, 9:46am UTC](https://discourse.vtk.org/t/vtkmetaimagereader-getting-additional-arbitrary-meta-tags/10728/4 "2023-02-20T09:46:48Z")

</div>

Yes, after vtkMetaImageWriter writes the file, you can use Python to rewrite the header with the extra information. Use an ordered dict and make sure ElementDataFile comes last:

```python
import collections
meta = collections.OrderedDict()

```

There’s also a metaimageio Python module (not in VTK, but on github and pypi), but I’ve never used it.

---

<div class="post-metadata">

**Author:** ![Ingmar](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/i/cc9497/32.png) [@Ingmar](https://discourse.vtk.org/u/Ingmar)\
**Post date:** [February 20, 2023, 9:13pm UTC](https://discourse.vtk.org/t/vtkmetaimagereader-getting-additional-arbitrary-meta-tags/10728/5 "2023-02-20T21:13:20Z")

</div>

Thanks!

Indeed [GitHub - auneri/MetaImageIO: Support for reading and writing images in MetaIO file format.](https://github.com/auneri/MetaImageIO) seems to be a great alternative, IMO it also overcomes some weaknesses of SimpleITK on that
