Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

What is the scipy.ndimage module in Python?

Sarvech Qadir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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')

RELATED TAGS

python
communitycreator

CONTRIBUTOR

Sarvech Qadir
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring