In today’s world, the most important thing is data. This data mostly consists of images as an important part. But before you can use them, it is necessary to process and analyze these digital images. In this way, you can improve their quality or extract some information from them to use. The processing of common images includes various tasks such as basic manipulations like cropping, flipping, rotating, etc. It also includes image segmentation, classification, image recognition, feature extractions; image restoration, etc. As we are discussing the Image library for Python so let’s focus on Python now.
Image library for Python
The best choice for the above-mentioned image processing tasks is Python. This is because of its increasing popularity as a scientific programming language and the free availability of many state-of-the-art image processing tools in its ecosystem. Let’s discuss the libraries through which most images can be processed and manipulation techniques can be carried out. With the help of these libraries, you can get easy and intuitive ways to transform images and make sense of the underlying data.
1. Python Imaging Library
Python Imaging Library also goes by the name of PIL. In the case of image manipulation, it is one of the core libraries in Python that is free of cost. However, its development has stagnated with its last release in 2009. We have found an actively developed fork of PIL called Pillow for you. It has many advantages such as easier installation, supports Python 3 and you can run it on all major operating systems. This library supports the features of image processing functionality, filtering with a set of built-in convolution kernels, and color-space conversions.
You need to install Pillow’s prerequisites before you install Pillow. The instructions for the platform can be easily found in Pillow installation instructions. Then, the command is as follows:
$ pip install Pillow
You can enhance an image in PIL using ImageFilter in the following way:
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")
2. Open Source Computer Vision
Open Source Computer Vision also goes by the name OpenCV. In comparison to PIL, OpenCV is a more advanced image manipulation and processing software. It consists of various languages and is being widely used for computer vision applications. As its background consists of code written in C/C++, OpenCV Python is very fast and along with that it is easy to code and deploy because of the Python wrapper in the foreground.
You can process image in Python using OpenCV by implementation of the
NumPy modules. You will easily be able to configure the project yourself by following the installation instructions. The following command can be used to download NumPy from the Python Package Index(PyPI):
$ pip install numpy
Here we are using Image Blending using Pyramids in OpenCV-Python to create an “Orapple”:
import cv2 #Read Image img = cv2.imread('testimg.jpg') #Display Image cv2.imshow('image',img) cv2.waitKey(0) cv2.destroyAllWindows() #Applying Grayscale filter to image gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #Saving filtered image to new file cv2.imwrite('graytest.jpg',gray)
scikit-image is an open source Python package which can work with NumPy arrays. You will find this library simple to use and straightforward even if you are new to Python’s ecosystem. It has the ability to implement algorithms and utilities for use in education, research, and industry applications. The code is written by an active community of volunteers in high-quality and is peer-reviewed.
There are various examples and practical use cases available in scikit-image.
The package is an import as skimage and you can easily find most of its functions in the submodules.
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 with the help of match_template function:
In Python programming, NumPy is considered as one of the core libraries and it supports arrays. You can call an image a standard NumPy array when it contains pixels of data points. So, you can use basic NumPy operations to modify the pixel values of an image. For instance, masking, slicing, fancy indexing, etc. You can load the image through skimage and display it using Matplotlib.
In the following way, you can use 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')
SimpleCV is one of the open source framework through which you can build computer vision. You can access various high-powered computer libraries through it. For instance, OpenCV but without knowing about file formats, color spaces, bit depths, etc. Its tagline says, “it’s computer vision made easy.” This explains that SimpleCV learning curve is essentially smaller than OpenCV. Some advantages of SimpleCV includes:
- The programmers who are only beginning can also write simple machine vision tests.
- Video files, images, cameras, and video streams are interoperable.
Pycairo is a set of Python bindings basically used for the Cairo graphics library. You can use its 2D graphics library for drawing vector graphics. Vector graphics are great as they don’t lose clarity even when they are resized or transformed. Also, Pycairo has the ability to call Cairo commands from Python.
Given below is some illustrations of drawing line, radial gradients and basic shapes using Pycairo:
We have discussed some of the best image processing libraries in Python. These image library in Python are very useful and most of them are free of cost. We hope that you try them out and find the one that is most convenient to you. Thank you for reading our blo