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.

8.5.13. nilearn.image.math_img

nilearn.image.math_img(formula, **imgs)[source]

Interpret a numpy based string formula using niimg in named parameters.

New in version 0.2.3.


The mathematical formula to apply to image internal data. It can use numpy imported as ‘np’.

imgsimages (Nifti1Image or file names)

Keyword arguments corresponding to the variables in the formula as Nifti images. All input images should have the same geometry (shape, affine).


Result of the formula as a Nifti image. Note that the dimension of the result image can be smaller than the input image. The affine is the same as the input image.

See also


To simply compute the mean of multiple images


This function is the Python equivalent of ImCal in SPM or fslmaths in FSL.


Let’s load an image using nilearn datasets module:

>>> from nilearn import datasets
>>> anatomical_image = datasets.load_mni152_template()

Now we can use any numpy function on this image:

>>> from nilearn.image import math_img
>>> log_img = math_img("np.log(img)", img=anatomical_image)

We can also apply mathematical operations on several images:

>>> result_img = math_img("img1 + img2",
...                       img1=anatomical_image, img2=log_img)