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
- PIL/ Pillow
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.
Scikit-learn has been well documented with many examples and practical use cases. Read the documentation here.
You can import the package as skimage and most functions are found within the submodules. Some examples of skimage include:
import matplotlib.pyplot as plt
from skimage import data,filters
image = data.coins()
# ... or any other NumPy array!
edges = filters.sobel(image)
You can do this by using the match_template function.
You can find more examples in the gallery.
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.
You can find a complete list of resources and documentation on NumPy’s official documentation page.
Mask an Image
import numpy as np
from skimage import data
import matplotlib.pyplot as plt
image = data.camera()
numpy.ndarray #Image is a numpy array
mask = image < 87
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.
For a complete list of functions provided by the scipy.ndimage package, refer to the documentation.
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)
plt.imshow(<image to be displayed>)
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.
The documentation has instructions for installation and examples covering every module of the library.
Enhance an Image Using ImageFilter
from PIL import Image, ImageFilter
im = Image.open( 'image.jpg' )
from PIL import ImageEnhance
enh = ImageEnhance.Contrast(im)
enh.enhance(1.8).show("30% more contrast")
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.
The OpenCV2-Python-Guide makes it easy to get started with OpenCV-Python.
Here’s an example that shows the capabilities of OpenCV-Python in image blending using pyramids to create a new fruit called orapple.
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
The official documentation is straightforward and has tons of examples and use cases to follow including the one below.
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.
The documentation contains installation instructions, examples and even some tutorials to help get started in Mahotas.
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.
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.
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.
The official Github Repository of PgMagick has instructions for installations and requirements as well as a detailed user guide.
A few image manipulation activities you can perform with PgMagick include:
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.
Draw Lines, Basic Shapes and Radial Gradients
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.