8

I have 3D array in Nifti file (.ii.gz) and I want to save it as a 3D numpy array. I used Nibabel to convert Numpy to Nifti1. Can I do the opposite?

Char
  • 1,635
  • 7
  • 27
  • 38

2 Answers2

22

From nipy

import numpy as np
import nibabel as nib

img = nib.load(example_filename)

a = np.array(img.dataobj)
piRSquared
  • 285,575
  • 57
  • 475
  • 624
8

You can also do this:

import numpy as np
import nibabel as nib

img_nifti = nib.load(filepath)
img = img_nifti.get_data()
schrodingercat
  • 246
  • 4
  • 21