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

read_img_data(img[, prefer])

Read data from image associated with files

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

guessed_image_type deprecated.2.1

  • deprecated from version: 4.0

Parameters
filenamestr

File name containing an image

Returns
image_classclass

Class corresponding to guessed image type

load

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

Load file given filename, guessing at file type

Parameters
filenamestring

specification of file to load

**kwargskeyword arguments

Keyword arguments to format-specific load

Returns
imgSpatialImage

Image of guessed type

read_img_data

nibabel.loadsave.read_img_data(img, prefer='scaled')

Read data from image associated with files

read_img_data deprecated.Please use img.dataobj.get_unscaled() instead.2.0.1

  • deprecated from version: 4.0

If you want unscaled data, please use img.dataobj.get_unscaled() instead. If you want scaled data, use img.get_data() (which will cache the loaded array) or np.array(img.dataobj) (which won’t cache the array). If you want to load the data as for a modified header, save the image with the modified header, and reload.

Parameters
imgSpatialImage

Image with valid image file in img.file_map. Unlike the img.get_data() method, this function returns the data read from the image file, as specified by the current image header and current image files.

preferstr, optional

Can be ‘scaled’ - in which case we return the data with the scaling suggested by the format, or ‘unscaled’, in which case we return, if we can, the raw data from the image file, without the scaling applied.

Returns
arrndarray

array as read from file, given parameters in header

Notes

Summary: please use the get_data method of img instead of this function unless you are sure what you are doing.

In general, you will probably prefer prefer='scaled', because this gives the data as the image format expects to return it.

Use prefer == ‘unscaled’ with care; the modified Analyze-type formats such as SPM formats, and nifti1, specify that the image data array is given by the raw data on disk, multiplied by a scalefactor and maybe with the addition of a constant. This function, with unscaled returns the data on the disk, without these format-specific scalings applied. Please use this funciton only if you absolutely need the unscaled data, and the magnitude of the data, as given by the scalefactor, is not relevant to your application. The Analyze-type formats have a single scalefactor +/- offset per image on disk. If you do not care about the absolute values, and will be removing the mean from the data, then the unscaled values will have preserved intensity ratios compared to the mean-centered scaled data. However, this is not necessarily true of other formats with more complicated scaling - such as MINC.

save

nibabel.loadsave.save(img, filename)

Save an image to file adapting format to filename

Parameters
imgSpatialImage

image to save

filenamestr

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?

which_analyze_type deprecated.2.1

  • deprecated from version: 4.0

Parameters
binaryblockbytes

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

Returns
hdr_typestr
  • 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