ImageFileError: Cannot work out file type of ".nii"

4.1k Views Asked by At

When I try to load my .NII file as a 4D Niimg-like object (I've tried both nilearn and nibabel),

I get the below error

Error: ImageFileError: Cannot work out file type of "/Users/audreyphan/Documents/Spring2020/DESPO/res4d/1/res4d_anat.nii"

Here is my code:

ds_name = '/Users/audreyphan/Documents/Spring2020/DESPO/res4d/1/res4d_anat.nii'
block = nib.load(ds_name) #Nibabel
block = image.load_img(ds_name) #Nilearn

Both attempts result in the same error.

I'm not sure what's causing this error to occur?

Thanks!

2

There are 2 best solutions below

0
On

It looks like the libraries are not able to extract the file type from your file. So first of all we have to be sure that the file is not corrupt. Therefore, can you load the data correctly with a tool such as ITK-SNAP (http://www.itksnap.org)?

If yes, you can try to define the file type by your own in the nibabel package by using the specific loader function. E.g. you can try each one of the following loader functions:

img_nifti1 = nib.Nifti1Image.from_filename(file)
img_nifti2 = nib.Nifti2Image.from_filename(file)
0
On

Oddly enough, this error also occurs when the access permissions are not set appropriately for the file you are trying to load. Try using chmod to change those access permissions appropriately and then loading the *.nii file.