NiBabel

Access a cacophony of neuro-imaging file formats

Table Of Contents

Previous topic

imageglobals

Next topic

orientations

Reggie -- the one

loadsave

Utilities to load and save image objects

guessed_image_type(filename) Guess image type from file filename
load(filename, **kwargs) Load file given filename, guessing at file type
save(img, filename) Save an image to file adapting format to filename
which_analyze_type(binaryblock) Is binaryblock from NIfTI1, NIfTI2 or Analyze header?

guessed_image_type

nibabel.loadsave.guessed_image_type(filename)

Guess image type from file filename

Parameters:

filename : str

File name containing an image

Returns:

image_class : class

Class corresponding to guessed image type

load

nibabel.loadsave.load(filename, **kwargs)

Load file given filename, guessing at file type

Parameters:

filename : string

specification of file to load

**kwargs : keyword arguments

Keyword arguments to format-specific load

Returns:

img : SpatialImage

Image of guessed type

save

nibabel.loadsave.save(img, filename)

Save an image to file adapting format to filename

Parameters:

img : SpatialImage

image to save

filename : str

filename (often implying filenames) to which to save img.

Returns:

None :

which_analyze_type

nibabel.loadsave.which_analyze_type(binaryblock)

Is binaryblock from NIfTI1, NIfTI2 or Analyze header?

Parameters:

binaryblock : bytes

The binaryblock is 348 bytes that might be NIfTI1, NIfTI2, Analyze, or None of the the above.

Returns:

hdr_type : str

  • a nifti1 header (pair or single) -> return ‘nifti1’
  • a nifti2 header (pair or single) -> return ‘nifti2’
  • an Analyze header -> return ‘analyze’
  • None of the above -> return None

Notes

Algorithm:

  • read in the first 4 bytes from the file as 32-bit int sizeof_hdr
  • if sizeof_hdr is 540 or byteswapped 540 -> assume nifti2
  • Check for ‘ni1’, ‘n+1’ magic -> assume nifti1
  • if sizeof_hdr is 348 or byteswapped 348 assume Analyze
  • Return None