Skip to main content

Read and write MetaImages with minimal dependencies

Project description

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 8-bit 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

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_quantized("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_quantized("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.

Project details


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.3.0.tar.gz (57.8 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.3.0-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tiny_metaio-0.3.0.tar.gz
  • Upload date:
  • Size: 57.8 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.3.0.tar.gz
Algorithm Hash digest
SHA256 5cc5daf1dc4a60e351edb00637e08f643846957b586807687549021381ef5ab1
MD5 eac17a081c034dd7341c284653ed7dd6
BLAKE2b-256 f13a7fab398b71308f47148ce18bbbab5fbd56ee4ce08b709a0bee085dee5f0b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tiny_metaio-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 11.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.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 76dc6f8de8c629435bc6fe5442865b9c2d2da2cc54e47313ea5d55fca3f46d66
MD5 d66e797c599cb9fb939aecb47903cb4a
BLAKE2b-256 f786af66299b5807fbd4da3f2ffbd39ef0284e79b18cd727ea36db7a4e89dd29

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 Pingdom Monitoring Sentry Error logging StatusPage Status page