Today’s world is full of data, and images make up a significant portion of this data. However, to be put to any use, these images need to be processed. Image processing is how we analyze and manipulate a digital image to improve its quality or extract information from it.

Typical tasks in image processing include displaying images, basic manipulations like cropping, flipping, rotating, etc., image segmentation, classification and feature extractions, image restoration, and image recognition. Due to Python’s growing popularity as a scientific programming language and the free availability of many state-of-art image processing tools in its ecosystem, it’s an apt choice for these image processing tasks. 

Let’s look at some of the commonly used Python libraries for image manipulation tasks.

10 Python Image Processing Libraries

  1. Scikit-Image
  2. NumPy
  3. SciPy
  4. PIL/ Pillow
  5. OpenCV-Python
  6. SimpleCV
  7. Mahotas
  8. SimpleITK
  9. PgMagick
  10. PyCairo

More From Our Python ExpertsPython Databases 101: How to Choose a Database Library

 

1. Scikit-Image

Scikit-image is an open-source Python package that works with NumPy arrays. It implements algorithms and utilities in research, education and industry applications. Scikit-image is a relatively straightforward library, even for those new to Python’s ecosystem. This code is high quality, peer-reviewed and written by an active community of volunteers.

Resources

Scikit-learn has been well documented with many examples and practical use cases. Read the documentation here.

Usage

You can import the package as skimage and most functions are found within the submodules. Some examples of skimage include:

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')
python-image-processing

Template Matching 

You can do this by using the match_template function.

python-image-processing

You can find more examples in the gallery.

 

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, we can modify the pixel values of an image. You can then load the image using skimage and display it using Matplotlib.

Resources

You can find a complete list of resources and documentation on NumPy’s official documentation page.

Usage

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')
python-image-processing

Shouldn’t Life Be Easier By Now?4 Python Tools to Simplify Your Life

 

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 provides functions operating on n-dimensional NumPy arrays. The package currently includes linear and non-linear filtering functions, 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

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

python-image-processing

 

4. PIL/ Pillow

PIL (Python Imaging Library) is a free library for the Python programming language that adds support for opening, manipulating and saving many different image file formats. However, its development has stagnated, with its last release in 2009. Fortunately, we have Pillow, an actively-developed fork of PIL which is easier to install, runs on all major operating systems and supports Python 3. The library contains basic image processing functionality, including point operations, filtering with a set of built-in convolution kernels and color space conversions.

Resources

The documentation has instructions for installation and examples covering every module of the library.

Usage

Enhance an Image Using ImageFilter

from PIL import Image, ImageFilter
#Read image
im = Image.open( 'image.jpg' )
#Display image
im.show()
 

from PIL import ImageEnhance
enh = ImageEnhance.Contrast(im)
enh.enhance(1.8).show("30% more contrast")
python-image-processing
Image source
Python Image Processing Tutorial for Beginners with Python PIL

 

5. OpenCV-Python

OpenCV (Open Source Computer Vision Library) is one of the most widely used libraries for computer vision applications. OpenCV-Python is the Python API for OpenCV. OpenCV-Python is not only fast since the background consists of code written in C/C++ but is also easy to code and deploy (due to the Python wrapper in the foreground). This makes it a great choice to perform computationally intensive computer vision programs.

Resources

The OpenCV2-Python-Guide makes it easy to get started with OpenCV-Python.

Usage

Image Blending

Here’s an example that shows the capabilities of OpenCV-Python in image blending using pyramids to create a new fruit called orapple.

python-image-processing

More on Computer VisionHow Do Self-Driving Cars Work?

 

6. SimpleCV

SimpleCV is also an open-source framework for building computer vision applications. With it, you get access to several high-powered computer vision libraries such as OpenCV   without having to first learn about bit depths, file formats, color spaces, etc. The learning curve is substantially smaller than that of OpenCV; as their tagline says, “it’s computer vision made easy.” Some points in favor of SimpleCV are:

  • Even beginning programmers can write simple machine vision tests

  • Cameras, video files, images and video streams are all interoperable

Resources

The official documentation is straightforward and has tons of examples and use cases to follow including the one below.

Usage

python-image-processing

 

7. Mahotas

Mahotas is another computer vision and image processing library for Python. It contains traditional image processing functions such as filtering, morphological operations and more modern computer vision functions for feature computation including interest point detection and local descriptors. The interface is in Python, which is appropriate for fast development, but the algorithms are implemented in C++ and are fine-tuned for speed. Mahotas library is fast with minimalistic code and even minimal dependencies. Read their official paper for more insights.

Resources

The documentation contains installation instructions, examples and even some tutorials to help get started in Mahotas.

Usage

Mahotas library relies on using simple code to get things done. For the Where’s Waldo problem, Mahotas does an excellent job, all with a minimum amount of code. Here’s the source code.

Clean Up Your Code5 Ways to Write More Pythonic Code

 

8. SimpleITK

ITK or Insight Segmentation and Registration Toolkit is an open-source, cross-platform system that provides developers with an extensive suite of software tools for image analysis. SimpleITK is a simplified layer built on top of ITK, intended to facilitate its use in rapid prototyping, education and interpreted languages. SimpleITK is an image analysis toolkit with many components supporting general filtering operations, image segmentation and registration. SimpleITK is written in C++ but is available for many programming languages, including Python.

Resources

There are a large number of Jupyter Notebooks illustrating the use of SimpleITK for educational and research activities out there. The notebooks demonstrate the use of SimpleITK for interactive image analysis using the Python and R programming languages.

Usage

The animation below visualizes a rigid CT/MR registration process created with SimpleITK and Python. Read the source code here.

 

9. PgMagick

PgMagick is a Python-based wrapper for the GraphicsMagick library. The GraphicsMagick Image Processing System is sometimes called the Swiss army knife of image processing. It provides a robust and efficient collection of tools and libraries which support reading, writing and image manipulation in over 88 major formats, including DPX, GIF, JPEG, JPEG-2000, PNG, PDF, PNM and TIFF.

Resources

The official Github Repository of PgMagick has instructions for installations and requirements as well as a detailed user guide.

Usage

A few image manipulation activities you can perform with PgMagick include:

Image Scaling

python-image-processing

Edge Extraction

python-image-processing

 

10. PyCairo

PyCairo is a set of Python bindings for the graphics library Cairo. Cairo is a 2D graphics library for drawing vector graphics. Vector graphics are interesting because they don’t lose clarity when you resize or transform them.

Resources

The PyCairo GitHub repository is a good resource with detailed instructions on installation and usage. You can also access a helpful guide with a brief PyCairo tutorial.

Usage

Draw Lines, Basic Shapes and Radial Gradients

python-image-processing

These are some of Python’s helpful and freely available image processing libraries. Some are relatively well-known, and some may be new for you. Try each of them out to see what will work best for your project. 

Expert Contributors

Built In’s expert contributor network publishes thoughtful, solutions-oriented stories written by innovative tech professionals. It is the tech industry’s definitive destination for sharing compelling, first-person accounts of problem-solving on the road to innovation.

Learn More

Great Companies Need Great People. That's Where We Come In.

Recruit With Us