8 Best Python Image Manipulation Tools
Want to extract underlying data from images? This article lists some of the best Python image manipulation tools that help you transform images.
Image by Editor
In today’s world, data plays a vital role in every industry vertical. Images can be one of the sources of extracting data. An image can be defined as a matrix of pixels, and each pixel represents a color that can be treated as a data value.
Image Processing comes in handy to uncover underlying data from any image. It helps you extract, manipulate, and filter data from an image. The main objective of image processing is to uncover some valuable information from images.
There are various applications of image processing, such as image sharpening, image restoration, pattern recognition, video processing, etc. Most image processing applications come under data analysis and data science.
And when it comes to data analysis, the only language that comes to our mind is Python. It is also the most preferred language for image processing because of its extensive set of libraries, which makes it very easy for developers to perform complex operations using simple lines of code.
Let’s have a look at some of the Python libraries which are primarily used for image processing.
8 Best Python Image Manipulation Tools
Here is a list of the best Python libraries that help you manipulate images easily. All of them are easy to use and allow you to extract the underlying data from images.
OpenCV (Open Source Computer Vision Library) is a popular Python Data Visualation library. It is an open-source library that is available for various programming languages, including C++, Java as well as assembly language.
This library was developed by Intel using the C++ programming language, and it was designed for real-time computer vision. It is ideal for executing computationally intensive computer vision programs.
As OpenCV is a third-party library, we can install it for our Python environment using the Python pip package manager tool.
pip install opencv-python
# import opencv import cv2 # Read the image image = cv2.imread('tesla.png') # grayscale the image gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cv2.imshow('Original Image', image) cv2.imshow('Grayscale Image', gray_image) cv2.waitKey(0) cv2.destroyAllWindows()
2. Pillow (PIL)
Pillow is another popular Python image processing library. It is the most basic image processing library that every beginner can start with. It is also known as PIL, which stands for Python Imaging Library.
PIL library comes with different file formatter extensions that provide powerful and complex features to perform image processing. If we compare PIL with OpenCV, PIL is a lightweight library with fewer features, making it easy to learn and handle for a new Python developer who has just entered the realm of image processing.
PIL is also a third-party open-source library, and it can be installed using the pip install command.
pip install pillow
GrayScale an Image in Python using Pillow
from PIL import Image with Image.open("tesla.png") as im: #show the original image im.show("Original Image") #convert into grayscale grayscaleImg = im.convert("L") #show the grayscale image grayscaleImg.show()
3. Scikit Image
Scikit Images is a scientifically inclined Python image-processing library. It is designed to process images using the Numpy and Scipy libraries. It includes various scientific algorithms, such as segmentation, color space manipulation, analysis, morphology, etc. This library is written using Python and C programming languages. It is available for all popular operating systems, such as Linux, macOS, and Windows.
scikit-image is an open-source library, and we can install it using the pip install command.
pip install scikit-image
GrayScale an image using the scikit-image library
from skimage import io from skimage.color import rgb2gray # way to load car image from file car = io.imread('tesla.png')[:,:,:3] #convert into grayscale grayscale = rgb2gray(car) #show the original io.imshow(car) io.show() #show the grayscale io.imshow(grayscale) io.show()
NumPy is the most basic Python scientific computing library. It is famous for introducing multidimensional arrays or matrices in Python. It is a dedicated scientific computing library. In addition, it comes with extensive mathematical features like arrays, linear algebra, basic statistical operations, random simulation, logical sorting, searching, shape manipulation, etc.
Again to install NumPy, we can use the pip install command.
pip install numpy
Grayscale the image using numpy
import numpy as np import matplotlib.pyplot as plt import matplotlib.image as mpimg #load the original image img_rgb = mpimg.imread('tesla.png')[...,:3] #show the original image plt.imshow(img_rgb) plt.show() #convert the image into grayscale img_gray = np.dot(img_rgb,[0.299, 0.587, 0.144]) #show the grayscale image plt.imshow(img_gray, cmap=plt.get_cmap('gray')) plt.show()
Similar to Numpy, SciPy is also a scientific computational library. It has more features than Numpy because it is built as an extension of the NumPy library.
Scipy provides high-level and complex commands and classes for data manipulation and data visualization. It covers a wide range of data processing tools. Also, it supports parallel programming, data access from the web, data-driven subroutines, and other mathematical features.
To install the SciPy library, we can take the help of the Python package manager CLI tool, pip.
pip install scipy
Convert an image in grayscale using scipy
from scipy import misc,ndimage from matplotlib import pyplot as plt import numpy as np img=misc.face() #show original image plt.imshow(img) plt.show() #grayscale using gaussian blur filter grayscale=ndimage.gaussian_filter(img,sigma=2) #show grayscale image plt.imshow(grayscale) plt.show()
Mahotas is yet another Python computer vision library that can perform various image processing operations. It is designed using C++, and it includes many algorithms to increase image processing speed. Also, it uses the image in a matrix using the NumPy array. Watershed, convex points calculations hit & miss convolution, and Sobel edges are the main features available in this library.
Mahotas is an open-source library and can be installed using the following terminal command.
pip install mahotas
Convert the RGB image to grayscale using Mahotas
import mahotas from pylab import imshow, show #read the image img = mahotas.imread('tesla.png') #show original image imshow(img) show() img = img[:, :, 0] grayscale = mahotas.overlay(img) #show grayscale image imshow(grayscale) show()
SimpleITK is a powerful toolkit for image registration and segmentation. It is built as an extension of the ITK toolkit for providing a simplified interface. It is available in different programming languages such as Python, R, C++, Java, C#, Ruby, TCL, and Lua.
This library supports 2D, 3D, and 4D images. The image processing speed of this library is very high compared to other Python image manipulation libraries and frameworks.
pip install SimpleITK
Load and show an image using SimpleITK
import SimpleITK as sitk import matplotlib.pyplot as plt logo = sitk.ReadImage('tesla.png') # GetArrayViewFromImage returns an immutable numpy array view to the data. plt.imshow(sitk.GetArrayViewFromImage(logo)) plt.show()
Matplotlib can also be used as an image processing library, although it is a data visualization library. It is generally used to plot the numpy array data, but it can also read the image data represented by NumPy arrays. We have already used the Matplotlib library in the above libraries to show and plot the images.
Matplotlib can be installed using the following simple command.
pip install matplotlib
# importing libraries. import matplotlib.pyplot as plt from PIL import Image # open image using pillow library image = Image.open("tesla.png") #show original image plt.imshow(image) plt.show() # grayscale the image plt.imshow(image.convert("L"), cmap='gray') plt.show()
Here ends our list of the best Python image manipulation tools. Among these eight libraries or tools, the most used Python image manipulation or processing libraries are Pillow and OpenCV (SimplICV in some specific cases).
If you are thinking of building a project related to image processing, such as identifying objects or color manipulation, consider using the OpenCV library because it is a huge library with lots of advanced features. The other libraries also support some image manipulation or processing features but are not that efficient.
Vijay Singh Khatri Graduate in Computer Science, specializing in Programming and Marketing. I am very fond of writing tech articles and creating new products.