How to read an Image in Python
Python provides many tools for processing image. In this article, we will see how to read an image in Python using OpenCV, Pillow module.
Using OpenCV
OpenCV (Open Source Computer Vision) is a computer vision library that contains various functions to perform operations on pictures or videos. This cross-platform library is available on multiple programming languages such as Python, C++ etc.
# importing OpenCV(cv2) module import cv2 # Save image in set directory # Read RGB image img = cv2.imread('Pythonpoint.net-logo.png') # Output img with window name as 'image-read' cv2.imshow('image-read', img) # Maintain output window until user presses a key cv2.waitKey(0) # Destroying present windows on screen cv2.destroyAllWindows()
Output:
Using Pillow module
Pillow is built on top of PIL (Python Image Library). PIL is one of the important modules for image processing in Python. However, the PIL module is not supported since 2011 and doesn’t support python 3.
# importing PIL from PIL import Image # Read image img = Image.open('Pythonpoint.net-logo.png') # Output Images img.show() # prints format of image print(img.format) # prints mode of image print(img.mode)
Output:
Subscribe
Login
Please login to comment
0 Discussion