

10 Python Image Manipulation Tools
Machine LearningModelingimage manipulationPythonposted by Parul Pandey May 8, 2020 Parul Pandey

This article looks at ten Python image manipulation tools. Our world today 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 for extracting some information from it which could then be put to some use.
[Related Article: 15+ Free and Paid Resources to Learn Python]
Common 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 availabiliy of many state of the art image processing tools in its ecosystem.
Let’s look at some of the commonly used Python libraries for image manipulation tasks.
1. scikit Image
scikit-image is an open-source Python package that works with numpy
arrays. It implements algorithms and utilities for use in research, education, and industry applications. It is a fairly simple and straightforward library even for those who are new to Python’s ecosystem. This code is of high-quality and peer-reviewed, written by an active community of volunteers.
Resources
It has been very well documented with a lot of examples and practical use cases. Read the documentation here.
Usage
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 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 match_template function
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. The image can be loaded using skimage and displayed using matplotlib.
Resources
A complete list of resources and documentation is available at 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
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 here.
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>)
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, 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.
Resources
The documentation has instructions for installation as well as examples covering every module of the library.
Usage
Enhancing an image in Pillow 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")
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
Here is an example which shows the capabilities of OpenCV-Python in Image Blending using Pyramids to create a new fruit called ‘Orapple’.
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, 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
Resources
The official documentation is very easy to follow and has tons of examples and use cases to follow.
Usage
7. Mahotas
Mahotas is another computer vision and image processing library for Python. It contains traditional image processing functions such as filtering and morphological operations as well as 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 tuned for speed. Mahotas library is fast with minimalistic code and even minimum dependencies. Read their official paper here for more insights.
Resources
The documentation contains installation instructions, examples and even some tutorials to help get started in mahotas easily.
Usage
Mahotas library relies on using simple code to get things done. For the problem of ‘Finding Wally’, Mahotas does a good job and that too with minimum amount of code. Here is the source 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. Among them, SimpleITK is a simplified layer built on top of ITK, intended to facilitate its use in rapid prototyping, education, interpreted languages. SimpleITK is an image analysis toolkit with a large number of components supporting general filtering operations, image segmentation, and registration. SimpleITK itself is written in C++ but is available for a large number of programming languages including Python.
Resources
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.
Usage
The animation below is a visualization of 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 manipulating an image in over 88 major formats including important formats like DPX, GIF, JPEG, JPEG-2000, PNG, PDF, PNM, and TIFF.
Resources
There is a Github Repository dedicated to PgMagick which has instructions for installations and requirements. There is also a detailed user guide on the subject.
Usage
Few image manipulation activities that can be performed with pgmagick are:
Image Scaling :
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 resized or transformed. Pycairo is a set of bindings for cairo which can be used to call cairo commands from Python.
Resources
The Pycairo GitHub repository is a good resource having detailed instructions on installation and usage. There is also a Getting Started guide which has a brief tutorial on Pycairo.
Usage
Drawing lines, basic shapes and radial gradients with Pycairo
Conclusion
These are some of the useful and freely available Image Processing libraries in Python. Some are fairly known and some may be new for you. Try them out to get to know more about them.
Originally Posted Here