Note

This page is a reference documentation. It only explains the function signature, and not how to use it. Please refer to the user guide for the big picture.

nilearn.image.copy_img

nilearn.image.copy_img(img)[source]

Copy an image to a nibabel.Nifti1Image.

Parameters:
imgimage

nibabel SpatialImage object to copy.

Returns:
img_copyNifti1Image

copy of input (data, affine and header)

Examples

>>> from nilearn.image import copy_img
>>> import numpy as np
>>> from nibabel import Nifti1Image
>>>
>>> # Create a dummy image.
>>> affine = np.eye(4)
>>> data = np.ones((3, 3, 3))
>>> img_3d = Nifti1Image(data, affine)
>>>
>>> # Copy the image vs reference assignment.
>>> img_3d_copy = copy_img(img_3d)
>>>
>>> # Use reference assignment.
>>> # This is not copying, img_3d_notcopy points to the
>>> # same object as img_3d!.
>>> img_3d_notcopy = img_3d
>>>
>>> # Show initial dtypes; they are all the same.
>>> img_3d.get_data_dtype()
dtype('<f8')
>>> img_3d_copy.get_data_dtype()
dtype('<f8')
>>> img_3d_notcopy.get_data_dtype()
dtype('<f8')
>>>
>>> # Change the dtype in the original image.
>>> img_3d.set_data_dtype("uint8")
>>>
>>> # Show the new dtypes.
>>> img_3d.get_data_dtype()
dtype('uint8')
>>>
>>> # img_3d_copy was copied
>>> # before the change and keeps the original dtype
>>> img_3d_copy.get_data_dtype()
dtype('<f8')
>>>
>>> # img_3d_notcopy refers to the same object as img_3d.
>>> # Hence its dtype has changed.
>>> img_3d_notcopy.get_data_dtype()
dtype('uint8')