Image Processing with SciPy and NumPy in Python

12 May, 2022
In this tutorial, we will discuss Image Processing in Python using the core scientific modules like NumPy and SciPy. The images are made up of NumPy ndarrays so we can process and manipulate images and SciPy provides the submodule scipy.ndimage that provides functions that can operate on the NumPy arrays.
 We will discuss how to open and write to images, and will also cover different manipulation and filtering techniques. So before getting started let’s see how to install both modules.
Numpy: To install numpy type the below command in the terminal.
pip install numpy
SciPy: You can use the above command to install SciPy as well.
pip install scipy
The misc package of SciPy comes with some preloaded images. We will use those images to learn about image processing. One such image is provided by the face() function. The face() function will get a colored image of a raccoon face.
Python3
from scipy import miscimport imageioimport matplotlib.pyplot as plt # reads a raccoon faceface = misc.face() # save the imageimageio.imsave('raccoon.png', face) plt.imshow(face)plt.show()
Output:
Here we will read the image using the imread() function.
Python3
from scipy import miscimport imageioimport matplotlib.pyplot as plt img = imageio.imread('raccoon.png') print(img.shape)print(img.dtype) plt.imshow(img)plt.show()
Output:
(768, 1024, 3)
uint8
A RAW file is a file containing minimally processed data from an image sensor. We can create this file using the tofile() method of the scipy package.
Python3
from scipy import miscimport imageioimport matplotlib.pyplot as plt # reads a raccoon faceface = misc.face() face.tofile("raccoon.raw")
This will create a .raw file in our current working directory.
For opening .raw file we will be needing the NumPy module that will use the fromfile() method. This function is an efficient way of reading binary data with known data type as well as parsing simply formatted text.
Python3
import numpy as np img = np.fromfile('raccoon.raw',                  dtype=np.uint8) print(img.shape)
Output:
(2359296,)
We can use the max() and min() functions to get the maximum and minimum along the given axis. And to find the mean we can use the mean() function.
Python3
from scipy import misc img = misc.face() print(img.max())print(img.min())print(img.mean())
Output:
255
0
110.16274388631184
As we know that images are represented by numbers in a matrix, so changing the value of the matrix will result in changing the original image. Let’s see how to use this idea for cropping the image.
Python3
from scipy import miscimport matplotlib.pyplot as plt # for grascaling the imageimg = misc.face(gray = True)  x, y = img.shape # Cropping the imagecrop = img[x//3: - x//8, y//3: - y//8] plt.imshow(crop)plt.show()
Output:
We can use the flipud() function of the numpy module to flip that image. This function flips the array(entries in each column) in up-down direction, shape preserved.
Python3
from scipy import miscimport numpy as npimport matplotlib.pyplot as plt img = misc.face() flip = np.flipud(img) plt.imshow(flip)plt.show()
Output:
To rotate the images we can use the ndarray.rotate() function. This function rotates the image at a specific angle.
Python3
from scipy import misc,ndimageimport matplotlib.pyplot as plt  img = misc.face() rotate = ndimage.rotate(face, 30) plt.imshow(rotate)plt.show()
Output:
In simpler terms image filtering is a process for enhancing or modifying an image where we can increase sharpness, enhance edges, or blurs the image. In Image filtering, some algorithm is applied to the pixel value of the given image and that algorithm determines the value of the output image. Let’s see some image filtering operations that can be done using NumPy and SciPy.
Blurring an image is a process of reducing the level of noise in the image. For this, we can either use a Gaussian filter or a unicorn filter.
Python3
from scipy import misc,ndimageimport matplotlib.pyplot as plt img = misc.face() blur_G = ndimage.gaussian_filter(img,sigma=7) plt.imshow(blur_G)plt.show()
Output:
Sharpening refers to increase contrast b/w light and dark regions and make the image more defined and brings out image features. There are three main reasons to sharpen your image: to overcome blurring introduced by camera equipment, to draw attention to certain areas, and to increase legibility. If blurry text is present in the image it becomes easy to read.
Python3
from scipy import misc, ndimageimport matplotlib.pyplot as plt img = misc.face(gray=True).astype(float)blur = ndimage.gaussian_filter(img, 5) # Showing Blur Imageplt.imshow(blur)plt.show() blur_G = ndimage.gaussian_filter(blur, 1)alpha = 30sharp = blur+alpha*(blur-blur_G) # showing sharp imagesplt.imshow(sharp)plt.show()
Output:
Denoising of an image refers to the process of reconstruction of a signal from noisy images. Denoising is done to remove unwanted noise from image to analyze it in better form. At first, let’s create a noisy image – 
Python3
from scipy import misc,ndimageimport matplotlib.pyplot as pltimport numpy as np img=misc.face(gray=True).astype(float)img=img[40:100,30:100] noise_img=img+0.9*img.std()*np.random.random(img.shape) plt.imshow(noise_img)plt.show()
Output:
To smooth the edges and the noise we use the Gaussian filter.
Python3
denoised = ndimage.gaussian_filter(noise_img, 2.2) plt.imshow(denoised)plt.show()
Output:
We can also preserve the edges using the median filter.
Python3
denoised = ndimage.median_filter(noise_img, 4) plt.imshow(denoised)plt.show()
Output:
The process of image detection involves detecting edges in the image. It works by detecting discontinuities in the brightness. For high-intensity variations, we can use Sobel, a gradient operator-
Python3
from scipy import misc, ndimageimport matplotlib.pyplot as pltimport numpy as np img = np.zeros((300, 300))img[64:-64, 64:-64] = 1img = ndimage.rotate(im, 30, mode='constant')img = ndimage.gaussian_filter(im, 7) # Original imageplt.imshow(im)plt.show() # edge detectionx = ndimage.sobel(im, axis=0, mode='constant')y = ndimage.sobel(im, axis=1, mode='constant')Sobel = np.hypot(x, y) plt.imshow(sob)plt.show()
Output:
sumitgumber28
Image-Processing
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.