Reading an image in OpenCV using Python - GeeksforGeeks

15 Dec, 2020
Prerequisite: Basics of OpenCV
In this article, we’ll try to open an image by using OpenCV (Open Source Computer Vision). To use the OpenCV library in python, we need to install these libraries as a prerequisite:
Numpy Library (Necessary, because OpenCV uses it in the background).OpenCV python 
Numpy Library (Necessary, because OpenCV uses it in the background).
OpenCV python 
To install these libraries, we need to run these pip commands in cmd:
pip install opencv-python
pip install numpy
pip install matplotlib
 To read the images cv2.imread() method is used. This method loads an image from the specified file. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format) then this method returns an empty matrix.
Syntax: cv2.imread(path, flag)
Parameters:path: A string representing the path of the image to be read.flag: It specifies the way in which image should be read. It’s default value is cv2.IMREAD_COLOR
Return Value: This method returns an image that is loaded from the specified file.
Note: The image should be in the working directory or a full path of image should be given.
All three types of flags are described below:
cv2.IMREAD_COLOR: It specifies to load a color image. Any transparency of image will be neglected. It is the default flag. Alternatively, we can pass integer value 1 for this flag.cv2.IMREAD_GRAYSCALE: It specifies to load an image in grayscale mode. Alternatively, we can pass integer value 0 for this flag.cv2.IMREAD_UNCHANGED: It specifies to load an image as such including alpha channel. Alternatively, we can pass integer value -1 for this flag.
Below codes are implementations to read images and display images on the screen using OpenCV and matplotlib libraries functions. Example #1 (Using OpenCV) : 
Image Used:
Python3
# Python code to read imageimport cv2 # To read image from disk, we use# cv2.imread function, in below method,img = cv2.imread("geeksforgeeks.png", cv2.IMREAD_COLOR) # Creating GUI window to display an image on screen# first Parameter is windows title (should be in string format)# Second Parameter is image arraycv2.imshow("Cute Kitens", img) # To hold the window on screen, we use cv2.waitKey method# Once it detected the close input, it will release the control# To the next line# First Parameter is for holding screen for specified milliseconds# It should be positive integer. If 0 pass an parameter, then it will# hold the screen until user close it.cv2.waitKey(0) # It is for removing/deleting created GUI window from screen# and memorycv2.destroyAllWindows()
Output:
Example #2: Opening in grascale mode
Python
# Python program to explain cv2.imread() method # importing cv2import cv2 # pathpath = r'geeksforgeeks.png' # Using cv2.imread() method# Using 0 to read image in grayscale modeimg = cv2.imread(path, 0) # Displaying the imagecv2.imshow('image', img)
Output : 
himanshukanojiya
Image-Processing
OpenCV
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Iterate over a list in Python
Python String | replace()