3 Python image processing tools

These Python libraries provide an easy and intuitive way to transform images and make sense of the underlying data.


Today’s world is full of data, and images form a significant part of this data. However, before they can be used, these digital images must be processed—analyzed and manipulated in order to improve their quality or extract some information that can be put to use.

Common image processing tasks include displays; basic manipulations like cropping, flipping, rotating, etc.; image segmentation, classification, and feature extractions; image restoration; and image recognition. Python is an excellent choice for these types of image processing tasks due to its growing popularity as a scientific programming language and the free availability of many state-of-the-art image processing tools in its ecosystem.

This article looks at 10 of the most commonly used Python libraries for image manipulation tasks. These libraries provide an easy and intuitive way to transform images and make sense of the underlying data.

1. scikit-image

scikit-image is very well documented with a lot of examples and practical use cases.

Usage

The package is imported as skimage, and most functions are found within the submodules.

Image filtering:

import matplotlib.pyplot as plt   
%matplotlib inline
        
from skimage import data,filters
        
image = data.coins()   # ... or any other NumPy array!  
edges = filters.sobel(image)  
plt.imshow(edges, cmap='gray')

Template matching using the match_template function:


2. NumPy

NumPy is one of the core libraries in Python programming and provides support for arrays. An image is essentially a standard NumPy array containing pixels of data points. Therefore, by using basic NumPy operations, such as slicing, masking, and fancy indexing, you can modify the pixel values of an image. The image can be loaded using skimage and displayed using Matplotlib.

Resources

A complete list of resources and documentation is available on NumPy’s official documentation page.

Usage

Using Numpy to mask an image:

import numpy as np
from skimage import data
import matplotlib.pyplot as plt 
%matplotlib inline
    
image = data.camera()  
type(image)
numpy.ndarray #Image is a NumPy array: 

mask = image < 87  
image[mask]=255  
plt.imshow(image, cmap='gray')

3. SciPy

SciPy is another of Python’s core scientific modules (like NumPy) and can be used for basic image manipulation and processing tasks. In particular, the submodule scipy.ndimage (in SciPy v1.1.0) provides functions operating on n-dimensional NumPy arrays. The package currently includes functions for linear and non-linear filtering, binary morphology, B-spline interpolation, and object measurements.

Resources

For a complete list of functions provided by the scipy.ndimage package, refer to the documentation.

Usage

Using SciPy for blurring using a Gaussian filter:

from scipy import misc,ndimage
    
face = misc.face()  
blurred_face = ndimage.gaussian_filter(face, sigma=3)  
very_blurred = ndimage.gaussian_filter(face, sigma=5)
   
#Results  
plt.imshow(<image to be displayed>)