vtkMetaImageReader: getting additional (arbitrary) meta tags?

MetaIO image files may contain various tags (such as https://itk.org/Wiki/ITK/MetaIO/Documentation#Associated_transformations and https://itk.org/Wiki/ITK/MetaIO/Documentation#Reference:_Tags_of_MetaImage) such as

CenterOfRotation = 20 30 40
AnatomicalOrientation = RAI

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

IntensityRange = 0 4095
InterceptSlope = -1024 1

However from the documentation (e.g. https://vtk.org/doc/nightly/html/classvtkMetaImageReader.html and 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

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 did not describe how to actually even get key names etc.

Please let me know

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:

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":

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

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:

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.


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