Reading images in Python
Python supports very powerful tools when comes to image processing. Let’s see how to process the images using different libraries like OpenCV, Matplotlib, PIL 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.
# Python program to read image using OpenCV# importing OpenCV(cv2) moduleimportcv2# 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()chevron_rightfilter_noneOutput :

- 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 program to read# image using matplotlib# importing matplotlib modulesimportmatplotlib.image as mpimgimportmatplotlib.pyplot as plt# Read Imagesimg=mpimg.imread('g4g.png')# Output Imagesplt.imshow(img)chevron_rightfilter_noneOutput :
- 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 program to read# image using PIL module# importing PILfromPILimportImage# Read imageimg=Image.open('g4g.png')# Output Imagesimg.show()# prints format of imageprint(img.format)# prints mode of imageprint(img.mode)chevron_rightfilter_noneOutput :
PNG
RGBA
Recommended Posts:
- Working with Images in Python
- Extract images from video in Python
- Python | Grayscaling of Images using OpenCV
- Python | Working with PNG Images using Matplotlib
- How to download Google Images using Python
- Apply changes to all the images in given folder - Using Python PIL
- Python | Uploading images in Django
- Python | Display images with PyGame
- Erosion and Dilation of images using OpenCV in python
- Get emotions of images using Microsoft emotion API in Python
- Python | OCR on All the Images present in a Folder Simultaneously
- Addition and Blending of images using OpenCV in Python
- Python | Denoising of colored images using opencv
- Python | Intensity Transformation Operations on Images
- Filtering Images based on size attributes in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : shubham_singh


