Reading images in Python - GeeksforGeeks

19 Feb, 2022
Python supports very powerful tools when comes to image processing. Let’s see how to process the images using different libraries like ImageIO, OpenCV, Matplotlib, PIL, etc. 
Using ImageIO : Imageio is a Python library that provides an easy interface to read and write a wide range of image data, including animated images, video, volumetric data, and scientific formats. It is cross-platform, runs on Python 3.7+, and is easy to install. It is the recommended alternative to scipy.misc.imread and is used internally by libraries like scikit-image to load images.
Using ImageIO : Imageio is a Python library that provides an easy interface to read and write a wide range of image data, including animated images, video, volumetric data, and scientific formats. It is cross-platform, runs on Python 3.7+, and is easy to install. It is the recommended alternative to scipy.misc.imread and is used internally by libraries like scikit-image to load images.
Python3
# Python program to read an write an image import imageio as iio # read an imageimg = iio.imread("g4g.png") # write it in a new formatiio.imwrite("g4g.jpg", img)
Output: 
Output: 
Output: 
Using OpenCV : OpenCV (Open Source Computer Vision) is a computer vision library that contains various functions to perform operations on pictures or videos. It was originally developed by Intel but was later maintained by Willow Garage and is now maintained by Itseez. This library is cross-platform that is it is available on multiple programming languages such as Python, C++ etc.  
Using OpenCV : OpenCV (Open Source Computer Vision) is a computer vision library that contains various functions to perform operations on pictures or videos. It was originally developed by Intel but was later maintained by Willow Garage and is now maintained by Itseez. This library is cross-platform that is it is available on multiple programming languages such as Python, C++ etc.  
Python3
# Python program to read image using OpenCV # importing OpenCV(cv2) moduleimport cv2 # Save image in set directory# Read RGB imageimg = cv2.imread('g4g.png') # Output img with window name as 'image'cv2.imshow('image', img) # Maintain output window utill# user presses a keycv2.waitKey(0)        # Destroying present windows on screencv2.destroyAllWindows()
Output : 
Output : 
Output : 
   Using MatplotLib : Matplotlib is an amazing visualization library in Python for 2D plots of arrays. 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.  
   
Using MatplotLib : Matplotlib is an amazing visualization library in Python for 2D plots of arrays. 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
# Python program to read# image using matplotlib # importing matplotlib modulesimport matplotlib.image as mpimgimport matplotlib.pyplot as plt # Read Imagesimg = mpimg.imread('g4g.png') # Output Imagesplt.imshow(img)
Output : 
Output : 
   Using PIL : PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. It was developed by Fredrik Lundh and several other contributors. Pillow is the friendly PIL fork and an easy to use library developed by Alex Clark and other contributors.  
   
   
Using PIL : PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. It was developed by Fredrik Lundh and several other contributors. Pillow is the friendly PIL fork and an easy to use library developed by Alex Clark and other contributors.  
Python
# Python program to read# image using PIL module # importing PILfrom PIL import Image # Read imageimg = Image.open('g4g.png') # Output Imagesimg.show() # prints format of imageprint(img.format) # prints mode of imageprint(img.mode)
Output :  
Output :  
Output :  
 
PNG
RGBA
 
shubham_singh
sebastian22
Image-Processing
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
How to Install PIP on Windows ?
Enumerate() in Python
Read a file line by line in Python
Iterate over a list in Python
Different ways to create Pandas Dataframe
Reading and Writing to text files in Python
Python OOPs Concepts
Create a Pandas DataFrame from Lists
*args and **kwargs in Python