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
See documentation for load and save functions for ‘working’ examples.
Coerce an Image into a new Image with a valid NIFTI coordmap so that it can be saved.
If standard is True, the resulting image has ‘standard’-ordered input_coordinates, i.e. ‘ijklmno’[:img.ndim]
Load an image from the given filename.
Load an image from the file specified by filename.
Parameters: | filename : string
mode : Either ‘r’ or ‘r+’ |
---|---|
Returns: | image : An Image object
|
See also
Examples
>>> from nipy.io.api import load_image
>>> from nipy.testing import anatfile
>>> img = load_image(anatfile)
>>> img.shape
(25, 35, 25)
Write the image to a file.
Parameters: | img : An Image object filename : string
|
---|---|
Returns: | image : An Image object |
See also
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, name = mkstemp(suffix='.nii.gz')
>>> tmpfile = open(name)
>>> saved_img = save_image(img, tmpfile.name)
>>> saved_img.shape
(91, 109, 91)
>>> tmpfile.close()
>>> os.unlink(name)