10 Python image manipulation tools. | by Parul Pandey | Towards Data Science

The content for this article has been adapted from my own article published previously in opensource.com.
Today's world is full of data, and images form a significant part of this data. However, to be put to any use, these images need to be processed. Image Processing is thus the process of analyzing and manipulating a digital image primarily aimed at improving its quality or extracting some information from it, which could then be used.
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. Python becomes an apt choice for such Image processing tasks. This is due to its growing popularity as a scientific programming language and the free availability of many State of Art Image Processing tools in its ecosystem.
Let's look at some of the commonly used Python libraries for Image manipulation tasks.
scikit-image is an open-source Python package that works with numpy arrays. It implements algorithms and utilities in research, education, and industry applications. It is a relatively straightforward library, even for those new to Python's ecosystem. This code is of high quality and peer-reviewed, written by an active community of volunteers.
It has been very well documented with many examples and practical use cases. Read the documentation here.
The package is imported as skimage And most functions are found within the submodules. Some examples of skimage include:
Image filtering
import matplotlib.pyplot as plt %matplotlib inlinefrom skimage import data,filtersimage = data.coins()# ... or any other NumPy array!edges = filters.sobel(image)plt.imshow(edges, cmap='gray')
Template Matching using 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. The image can be loaded using skimage and displayed using matplotlib.
A complete list of resources and documentation is available on Numpy's official documentation page.
Using Numpy to mask an image.
import numpy as npfrom skimage import dataimport matplotlib.pyplot as plt %matplotlib inlineimage = data.camera()type(image)numpy.ndarray #Image is a numpy arraymask = image < 87image[mask]=255plt.imshow(image, cmap='gray')
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.ndimageprovides 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 here.
Using SciPy for blurring using a Gaussian filter:
from scipy import misc,ndimageface = misc.face()blurred_face = ndimage.gaussian_filter(face, sigma=3)very_blurred = ndimage.gaussian_filter(face, sigma=5)#Resultsplt.imshow(<image to be displayed>)
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, there is 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.
Enhancing an image in Pillow using ImageFilter:
from PIL import Image, ImageFilter#Read imageim = Image.open( 'image.jpg' )#Display imageim.show()from PIL import ImageEnhanceenh = 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 is 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, and 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 to follow and has tons of examples and use cases to follow.
Mahotas is another computer vision and image processing library for Python. It contains traditional image processing functions such as filtering and morphological operations and more modern computer vision functions for feature computation, including interest point detection and local descriptors. The interface is in Python, appropriate for fast development, but the algorithms are implemented in C++ and are finetuned for speed. Mahotas library is fast with minimalistic code and even minimum dependencies. Read their official paper here for more insights.
The documentation contains installation instructions, examples, and even some tutorials to help get started in Mahotas.
Mahotas library relies on using simple code to get things done. For the problem of 'Finding Wally,' Mahotas does an excellent job, and that too with a minimum amount of code. Here is the source code.
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.
A large number of Jupyter Notebooks illustrating the use of SimpleITK for educational and research activities have been provided. The notebooks demonstrate the use of SimpleITK for interactive image analysis using the Python and R programming languages.
The animation below visualizes a rigid CT/MR registration process created with SimpleITK and Python. Read the source code here.
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 manipulating an image in over 88 major formats, including important formats like DPX, GIF, JPEG, JPEG-2000, PNG, PDF, PNM, and TIFF.
The official Github Repository of PgMagick has instructions for installations and requirements. There is also a detailed user guide on the subject.
Few image manipulation activities that can be performed with pgmagick are:
Image Scaling :
Edge extraction:
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 resized or transformed. Pycairo is a set of bindings for Cairo in Python.
The Pycairo GitHub repository is a good resource having detailed instructions on installation and usage. There is also a Getting started guide with a brief tutorial on Pycairo.
Drawing lines, basic shapes, and radial gradients with Pycairo
These are some of Python's helpful and freely available Image Processing libraries. Some are relatively known, and some may be new for you. Try them out to get to know more about them.