What is the scipy.ndimage module in Python?

Scipy ndimage (n- dimensional image) is a widely used module for image processing.

Below are among the most common tasks performed in image processing using ndimage:

  1. Input, outputting, and displaying images.

  2. Cropping an image, inverting an image, rotating an image, etc.

  3. Image filtering operations such as denoising or sharpening an image.

  4. Image Classification.

  5. Feature extraction.

Input and output images

from scipy import misc
import matplotlib.pyplot as plt
img = misc.face()
plt.imshow(img)
plt.show()
## lets look at some statistics like, mean, max, min values
print(img.mean(), img.max(), img.min())

All images in Python are numbers ranging between 0 and 255. Lets perform a few operationslike cropping an image, inverting an image, rotating an image, or blurring an image by applying filters. This is shown below:

import matplotlib.pyplot as plt
import numpy as np
import scipy.ndimage as ndimage
from scipy import misc
img = misc.face()
# Cropping an image
x = img.shape[0]
y = img.shape[1]
cropped = img[int(x / 4): int(- x / 4), int(y / 4): int(- y / 4)]
plt.imshow(cropped)
plt.show()
plt.savefig('output/1.png')
# inverted image
inverted = np.flipud(img)
plt.imshow(inverted)
plt.show()
plt.savefig('output/2.png')
# rotation
rotating_by_45_degrees = ndimage.rotate(img, 45)
plt.imshow(rotating_by_45_degrees)
plt.show()
plt.savefig('output/3.png')
# blurring using gaussian filter
blurred = ndimage.gaussian_filter(img, sigma=8)
plt.imshow(blurred)
plt.show()
plt.savefig('output/4.png')

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved