NIPY logo

Site Navigation

NIPY Community

Table Of Contents

Previous topic

interfaces.spm

Next topic

io.imageformats.analyze

This Page

io.files

Module: io.files

The image module provides basic functions for working with images in nipy. Functions are provided to load, save and create image objects, along with iterators to easily slice through volumes.

load : load an image from a file

save : save an image to a file

fromarray : create an image from a numpy array

Examples

See documentation for load and save functions for ‘working’ examples.

Functions

nipy.io.files.as_image(image_input)

Load image from filename or pass through image instance

Parameters :

image_input : str or Image instance

image or string filename of image. If a string, load image and return. If an image, pass through without modification

Returns :

img : Image or Image-like instance

Input object if image_input seemed to be an image, loaded Image object if image_input was a string.

Raises :

TypeError : if neither string nor image-like passed

Examples

>>> from nipy.testing import anatfile
>>> from nipy.io.api import load_image
>>> img = as_image(anatfile)
>>> img2 = as_image(img)
>>> img2 is img
True
nipy.io.files.load(filename)

Load an image from the given filename.

Parameters :

filename : string

Should resolve to a complete filename path.

Returns :

image : An Image object

If successful, a new Image object is returned.

See also

save_image
function for saving images
fromarray
function for creating images from numpy arrays

Examples

>>> from nipy.io.api import load_image
>>> from nipy.testing import anatfile
>>> img = load_image(anatfile)
>>> img.shape
(33, 41, 25)
nipy.io.files.save(img, filename, dtype=None)

Write the image to a file.

Parameters :

img : An Image object

filename : string

Should be a valid filename.

Returns :

image : An Image object

See also

load_image
function for loading images
fromarray
function for creating images from numpy arrays

Notes

Filetype is determined by the file extension in ‘filename’. Currently the following filetypes are supported:

  • Nifti single file : [‘.nii’, ‘.nii.gz’]
  • Nifti file pair : [‘.hdr’, ‘.hdr.gz’]
  • Analyze file pair : [‘.img’, ‘img.gz’]

Examples

>>> import os
>>> import numpy as np
>>> from tempfile import mkstemp
>>> from nipy.core.api import fromarray
>>> from nipy.io.api import save_image
>>> data = np.zeros((91,109,91), dtype=np.uint8)
>>> img = fromarray(data, 'kji', 'zxy')
>>> fd, fname = mkstemp(suffix='.nii.gz')
>>> saved_img = save_image(img, fname)
>>> saved_img.shape
(91, 109, 91)
>>> os.unlink(fname)
>>> fd, fname = mkstemp(suffix='.img.gz')
>>> saved_img = save_image(img, fname)
>>> saved_img.shape
(91, 109, 91)
>>> os.unlink(fname)
>>> fname = 'test.mnc'
>>> saved_image = save_image(img, fname)
Traceback (most recent call last):
   ...
ValueError: Cannot save file type "minc"