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.
Load image from filename or pass through image instance
Parameters : | image_input : str or Image instance
|
---|---|
Returns : | img : Image or Image-like instance
|
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
Load an image from the given filename.
Parameters : | filename : string
|
---|---|
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
(33, 41, 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:
Examples
Make a temporary directory to store files
>>> import os
>>> from tempfile import mkdtemp
>>> tmpdir = mkdtemp()
Make some some files and save them
>>> import numpy as np
>>> 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')
>>> fname1 = os.path.join(tmpdir, 'img1.nii.gz')
>>> saved_img1 = save_image(img, fname1)
>>> saved_img1.shape
(91, 109, 91)
>>> fname2 = os.path.join(tmpdir, 'img2.img.gz')
>>> saved_img2 = save_image(img, fname2)
>>> saved_img2.shape
(91, 109, 91)
>>> fname = 'test.mnc'
>>> saved_image3 = save_image(img, fname)
Traceback (most recent call last):
...
ValueError: Cannot save file type "minc"
Finally, we clear up our temporary files:
>>> import shutil
>>> shutil.rmtree(tmpdir)