Image Processing with SciPy and NumPy in Python
Last Updated :
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.
Installation
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

Opening and Writing Images
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.
Example: Saving image using SciPy
Python3
from scipy import misc
import imageio
import matplotlib.pyplot as plt
# reads a raccoon face
face = misc.face()
# save the image
imageio.imsave('raccoon.png', face)
plt.imshow(face)
plt.show()
Output:

Example: Creating NumPy array from the image
Here we will read the image using the imread() function.
Python3
from scipy import misc
import imageio
import 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

Creating RAW file
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.
Example: Creating RAW file using SciPy
Python3
from scipy import misc
import imageio
import matplotlib.pyplot as plt
# reads a raccoon face
face = misc.face()
face.tofile("raccoon.raw")
This will create a .raw file in our current working directory.
Opening RAW File
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.
Example: Reading from RAW file using NumPy
Python3
import numpy as np
img = np.fromfile('raccoon.raw',
dtype=np.uint8)
print(img.shape)
Output:
(2359296,)
Getting Statistical Information
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.
Example: Getting min, max, and mean values
Python3
from scipy import misc
img = misc.face()
print(img.max())
print(img.min())
print(img.mean())
Output:
255
0
110.16274388631184
Cropping Image
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.
Example: Cropping the image
Python3
from scipy import misc
import matplotlib.pyplot as plt
# for grascaling the image
img = misc.face(gray = True)
x, y = img.shape
# Cropping the image
crop = img[x//3: - x//8, y//3: - y//8]
plt.imshow(crop)
plt.show()
Output:

Flipping Images
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.
Example: Flip Image using Scipy
Python3
from scipy import misc
import numpy as np
import matplotlib.pyplot as plt
img = misc.face()
flip = np.flipud(img)
plt.imshow(flip)
plt.show()
Output:

Rotating Images
To rotate the images we can use the ndarray.rotate() function. This function rotates the image at a specific angle.
Example: Rotate Image using SciPy and NumPy
Python3
from scipy import misc,ndimage
import matplotlib.pyplot as plt
img = misc.face()
rotate = ndimage.rotate(face, 30)
plt.imshow(rotate)
plt.show()
Output:

Filtering Images
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 Images
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.
Example: Blur Images using SciPy and NumPy
Python3
from scipy import misc,ndimage
import matplotlib.pyplot as plt
img = misc.face()
blur_G = ndimage.gaussian_filter(img,sigma=7)
plt.imshow(blur_G)
plt.show()
Output:

Sharpening Images
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.
Example: Sharpening images using NumPy and SciPy
Python3
from scipy import misc, ndimage
import matplotlib.pyplot as plt
img = misc.face(gray=True).astype(float)
blur = ndimage.gaussian_filter(img, 5)
# Showing Blur Image
plt.imshow(blur)
plt.show()
blur_G = ndimage.gaussian_filter(blur, 1)
alpha = 30
sharp = blur+alpha*(blur-blur_G)
# showing sharp images
plt.imshow(sharp)
plt.show()
Output:

Denoising Images
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 -Â
Example 1: Creating a noisy image
Python3
from scipy import misc,ndimage
import matplotlib.pyplot as plt
import 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:

Edge Detection
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-
Example: Edge detection using SciPy and NumPy
Python3
from scipy import misc, ndimage
import matplotlib.pyplot as plt
import numpy as np
img = np.zeros((300, 300))
img[64:-64, 64:-64] = 1
img = ndimage.rotate(im, 30, mode='constant')
img = ndimage.gaussian_filter(im, 7)
# Original image
plt.imshow(im)
plt.show()
# edge detection
x = 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:

Similar Reads
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is
8 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read