pynrrd

raw JSON →
1.1.3 verified Mon Apr 27 auth: no python

Pure Python library for reading and writing NRRD (Nearly Raw Raster Data) files, compatible with NumPy. Version 1.1.3 supports Python >=3.7 and NumPy >=1.21. Release cadence is sporadic, with occasional maintenance updates.

pip install pynrrd
error ImportError: No module named pynrrd
cause Attempting to import pynrrd directly; the module is named nrrd.
fix
Use 'import nrrd' instead of 'import pynrrd'.
error NRRDError: 'Size of the data does not equal to the product of all dimensions'
cause The data in the NRRD file is corrupted or the header dimensions do not match the actual data size.
fix
Verify the file integrity and ensure the header dimensions (sizes) match the data.
error ValueError: cannot read from a closed file
cause Trying to use a file object that has been closed before reading.
fix
Do not close the file object before nrrd.read() finishes. If using a context manager, read inside the with block.
error TypeError: numpy() takes at most 1 argument (2 given) (when using np.array(data))
cause If the data is already a numpy array, np.array(data) is fine; this error may occur with other misuse.
fix
Ensure you pass a proper array-like object. For a read-only array, just use data.copy() if needed.
breaking pynrrd v1.0.0 dropped support for Python 2 and Python 3.6 and below.
fix Upgrade to Python 3.7+ and use pynrrd >=1.0.0.
breaking v1.1.0 dropped support for NumPy <1.21. If you use an older NumPy, pynrrd 1.1.0+ will not install.
fix Upgrade NumPy to >=1.21.
gotcha nrrd.read() returns the data as a numpy array. If the file is compressed, the array may be read-only. To modify it, use np.array(data).
fix Call np.asarray(data) or data.copy() to get a writable array.
gotcha Using pip install pynrrd while the import is import nrrd can confuse users. The package name on PyPI is pynrrd, but the module is nrrd.
fix Always use 'import nrrd' (not 'import pynrrd').

Basic read/write example using numpy array.

import nrrd
import numpy as np

# Write a NRRD file
data = np.random.rand(10, 10, 10)
nrrd.write('example.nrrd', data)

# Read a NRRD file
read_data, header = nrrd.read('example.nrrd')
print(header)