Python provides simple tools to work with images. Whether you're looking to open, view or save images there are many libraries to help. Let's see how to process the images using different libraries.
1. Using ImageIO
ImageIO is used for reading and writing images in various formats like PNG, JPEG, GIF, TIFF and more. It's particularly useful for scientific and multi-dimensional image data likie medical images or animated GIFs. It’s simple and works across different platforms. You can download the image from here.
Python
import imageio.v2 as iio
import matplotlib.pyplot as plt
img = iio.imread("g4g.png")
plt.imshow(img)
Output:
ImageIO2. Using OpenCV
OpenCV (Open Source Computer Vision Library) is one of the most popular tools for real-time computer vision. It supports image processing, face detection, video analysis, object detection and much more. It can Can apply filters, transformations and can bes used for face/object recognition. This library is cross-platform that is it is available on multiple programming languages such as Python, C++, etc.
Python
import cv2
img = cv2.imread('g4g.png')
cv2.imshow('image', img)
Output:
OpenCV3. Using Matplotlib
Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. It was introduced by John Hunter in the year 2002. Matplotlib comes with a wide variety of plots. Plots helps to understand trends, patterns and to make correlations. They’re typically instruments for reasoning about quantitative information.
Python
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
img = mpimg.imread('g4g.png')
plt.imshow(img)
Output:
Matplotlib4. Using PIL
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. Pillow is user friendly and easy to use library developed by Alex Clark and other contributors.
Python
from PIL import Image
img = Image.open('g4g.png')
img.show()
print(img.mode)
Output:
PILThese Python libraries make image reading and processing easy and efficient for all kinds of tasks. Choose the one that best fits your needs and start exploring image data with just a few lines of code.
Similar Reads
Reading binary files in Python Reading binary files means reading data that is stored in a binary format, which is not human-readable. Unlike text files, which store data as readable characters, binary files store data as raw bytes. Binary files store data as a sequence of bytes. Each byte can represent a wide range of values, fr
5 min read
Read File As String in Python Python provides several ways to read the contents of a file as a string, allowing developers to handle text data with ease. In this article, we will explore four different approaches to achieve this task. Each approach has its advantages and uses cases, so let's delve into them one by one. Read File
3 min read
Read JSON file using Python The full form of JSON is JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called JSON. To use this feature, we import the JSON package in Pytho
4 min read
Unpacking arguments in Python If you have used Python even for a few days now, you probably know about unpacking tuples. Well for starter, you can unpack tuples or lists to separate variables but that not it. There is a lot more to unpack in Python. Unpacking without storing the values: You might encounter a situation where you
3 min read
Python | os.read() method OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. os.read() method in Python is used to read at most n bytes from the file associat
2 min read