10 Python image manipulation tools.

10 Python image manipulation tools.

An overview of some of the commonly used Python libraries that provide an easy and intuitive way to transform images.

Parul PandeyBlockedUnblockFollowFollowingMay 25Photo by Luriko Yamaguchi from PexelsThe content for this article has been adapted from my own article published previously in opensource.

com.

IntroductionOur 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 analysing 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.

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 availability 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 Imagescikit-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.

ResourcesIt has been very well documented with a lot of examples and practical use cases.

Read the documentation here.

UsageThe package is imported as skimage and most functions are found within the submodules.

Some examples of skimage include:Image filteringimport 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 functionYou can find more examples in the gallery.

2.

NumpyNumpy 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.

ResourcesA complete list of resources and documentation is available at Numpy’s official documentation page.

UsageUsing 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')3.

Scipyscipy 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.

ResourcesFor a complete list of functions provided by the scipy.

ndimage package, refer to the documentation here.

UsageUsing 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>)4.

PIL/ PillowPIL( 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 colour space conversions.

ResourcesThe documentation has instructions for installation as well as examples covering every module of the library.

UsageEnhancing 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")Source5.

OpenCV-PythonOpenCV (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 foreground).

This makes it a great choice to perform computationally intensive computer vision programs.

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

UsageHere is an example which shows the capabilities of OpenCV-Python in Image Blending using Pyramids to create a new fruit called ‘Orapple’.

Source6.

SimpleCVSimpleCV 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 favour of SimpleCV are:Even beginning programmers can write simple machine vision testsCameras, video files, images, and video streams are all interoperableResourcesThe official documentation is very easy to follow and has tons of examples and use cases to follow.

Usage7.

MahotasMahotas 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.

ResourcesThe documentation contains installation instructions, examples and even some tutorials to help get started in mahotas easily.

UsageMahotas 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.

Source8.

SimpleITKITK 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.

ResourcesA 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.

UsageThe animation below is a visualization of a rigid CT/MR registration process created with SimpleITK and Python.

Read the source code here.

Source9.

pgmagickpgmagick 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.

ResourcesThere is a Github Repository dedicated to PgMagick which has instructions for installations and requirements.

There is also a detailed user guide on the subject.

UsageFew image manipulation activities that can be performed with pgmagick are:Image Scaling :Edge extraction:10.

PycairoPycairo 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.

ResourcesThe 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.

UsageDrawing lines, basic shapes and radial gradients with PycairoConclusionThese 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.

. More details

Leave a Reply