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
numpyas 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5cc5daf1dc4a60e351edb00637e08f643846957b586807687549021381ef5ab1
|
|
| MD5 |
eac17a081c034dd7341c284653ed7dd6
|
|
| BLAKE2b-256 |
f13a7fab398b71308f47148ce18bbbab5fbd56ee4ce08b709a0bee085dee5f0b
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
76dc6f8de8c629435bc6fe5442865b9c2d2da2cc54e47313ea5d55fca3f46d66
|
|
| MD5 |
d66e797c599cb9fb939aecb47903cb4a
|
|
| BLAKE2b-256 |
f786af66299b5807fbd4da3f2ffbd39ef0284e79b18cd727ea36db7a4e89dd29
|