Import Numpy File in as image volume

I have been working with trying to compress files down to NPY and NPZ files to save binary labelmaps for some of my data. Is there a way to import them into slicer? Every time I try the load data option with it, it doesn’t seem to want to load into the scene?

NPY/NPZ file format is not widely supported outside numpy and I’m not sure if there is a standardized way of specifying essential metadata. In contrast, existing standard medical image computing file formats don’t have these issues. Since we already have more file formats than we need, it would be better not to introduce new ones. You can of course use NPY/NPZ for temporary, application-specific storage and we may consider adding readers/writers for these files if there is a standard way of using them for medical image storage.

Instead of saving as NPY/NPZ, it is possible to ITK to save the image in a file format supported by Slicer.

For example

$ python -m pip install --upgrade pip
$ python -m pip install itk
$ python
>>> [... generate numpy array `arr`]
>>> import itk
>>> image = itk.GetImageFromArray(arr)
>>> itk.imwrite(image, 'image.nrrd')

Hope this helps,
Matt

1 Like