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.
It has been very well documented with a lot of 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 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.
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 at Numpy’s official documentation page.
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')
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 functions for linear and non-linear filtering, 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,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.
The documentation has instructions for installation as well as examples covering every module of the library.
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")
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 which 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 very easy 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 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.
The documentation contains installation instructions, examples and even some tutorials to help get started in mahotas easily.
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.
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 is a visualization of 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.
Few image manipulation activities that can be performed with pgmagick are:
Image Scaling :
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.
Drawing lines, basic shapes and radial gradients with Pycairo
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