Skip to main content

MetaIO

Read MetaImages (.mha or .mhd) and NIFTI images (.nii or .nii.gz) and write .mha images in python with minimal dependencies.

Bonus: provides a custom MHA-like format when size matters: .metaq.

Installation

Available on pypi.org: pip install tiny-metaio.

Usage

Writing a MHA file

>>> import metaio
>>> img = metaio.MetaImage([[0, 1], [42, 43]], spacing=[1, 2])
>>> img.save("some-name.mha")

Reading a MHA file

>>> img = metaio.read("some-name.mha")
>>> img.spacing
array([1., 2.])
>>> img.data
array([[ 0,  1],
       [42, 43]])
>>> img.direction
array([1., 0., 0., 1.])
>>> img.origin
array([0., 0.])

Custom format

Integers

Integers are stored without loss in a very compact way.

>>> from pathlib import Path
>>> import numpy as np
>>> img = metaio.MetaImage([[-5] * 1000, [2] * 1000])
>>> img.save("some-name.mha")
>>> Path("some-name.mha").stat().st_size
16246
>>> img.save_compact("some-name.metaq")
>>> round(Path("some-name.metaq").stat().st_size / Path("some-name.mha").stat().st_size, 2)
0.09
>>> np.all(metaio.read("some-name.metaq").data == img.data)
np.True_

Floats

You need to specify a range of values covered by the quantization. Values outside this ranges will be clipped. A loss of precision is expected. NaNs will be implicitly converted to zero.

>>> img = metaio.MetaImage([[-0.5, 1], [2.5, 5]])
>>> img.save_compact("some-name.metaq", min_=0, max_=2.5)
>>> metaio.read("some-name.metaq").data
array([[0. , 1. ],
       [2.5, 2.5]], dtype=float32)

Additionally, if some values do not matter, you can specify a mask for further compression. The mask must be a binary array of the same shape as the image. Values where the mask is False will not be stored at all and restored as NaN on dequantization.

>>> img = metaio.MetaImage([[-0.5, 1], [2.5, 5]])
>>> img.save_compact("some-name.metaq", min_=0, max_=2.5, mask=img.data <= 2.5)
>>> metaio.read("some-name.metaq").data
array([[0. , 1. ],
       [2.5, nan]], dtype=float32)

Command-line interface

A command-line interface is available to convert supported input format to a MHA.

$ metaio /path/to/input.metaq /path/to/output.mha

Philosophy

  • numpy as only runtime dependency.
  • idiomatic python.
  • similar behavior than SimpleITK's GetArrayFromImage, GetImageFromArray, GetDirection, GetOrigin, GetSpacing, ReadImage, WriteImage.

Why should I use this over SimpleITK?

If you just need the IO parts and do not want the large SimpleITK package, or if you need to efficiently store some large MetaImage-like data, this package might be for you.

If you do not care about having SimpleITK as a dependency for your project, this package is not for you.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tiny_metaio-0.4.0.tar.gz (62.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tiny_metaio-0.4.0-py3-none-any.whl (16.8 kB view details)

Uploaded Python 3

File details

Details for the file tiny_metaio-0.4.0.tar.gz.

File metadata

  • Download URL: tiny_metaio-0.4.0.tar.gz
  • Upload date:
  • Size: 62.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for tiny_metaio-0.4.0.tar.gz
Algorithm Hash digest
SHA256 edd15a22d14638d05ae961126284c8899c1156db9adb1c6cd70ab3fa15539250
MD5 1934be3e779aa5cdca5e9714d45eb3ad
BLAKE2b-256 ec1a863015abaed4fa7ad3da21db8fa7ceb50732c2290bb3f62f0a61aae3667f

See more details on using hashes here.

File details

Details for the file tiny_metaio-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: tiny_metaio-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 16.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for tiny_metaio-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4b7d4d959470c735876ef9e304a6a7bebb713aa4393b2c1fc5e3a29966c9bde8
MD5 3e4db197bf85b1d1e1e99f2654d85cfd
BLAKE2b-256 645645ed0a93bf9f83e261d633eea6d07092dbc505f97a72bf81dcfdaf302a02

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page