Python supports a large collection of modern libraries to perform difficult real-world tasks, such as OpenCV for Computer Vision, TensorFlow for machine learning, Pandas and Numpy for mathematical computation, etc. The Python OpenCV library is widely used in image processing and computer vision projects like real-time face and hand detection, object tracking, etc.
In this write-up, the Python OpenCV function named “cv2.imread()” is briefly explained with numerous examples. The following contents are briefly demonstrated in this article:
- What is Python cv2.imread()?
- Default Reading Image Using cv2.imread()
- Reading the Image in Grayscale Using cv2.imread()
So, let’s get started!
What is Python cv2.imread()?
The “cv2.imread()” function of Python OpenCV is used to load images from the given path. The “cv2.imread()” returns the NumPy array that contains pixel value. The syntax of “cv2.imread()” function is shown below:
cv2.imread(path, flag)
The “cv2.imread()” takes two parameter values as a path and a flag. The “path” parameter specifies the path/location of the picture. While the “flag” parameter is optional to define. If defined, one of the following values can be passed:
- cv2.IMREAD_COLOR: This is the default value for the “flag” parameter. It depicts that read image in RGB color without any transparency channel.
- cv2.IMREAD_GRAYSCALE: Pass this value in place of the flag parameter to convert the image into grayscale. The value “0” can also be passed to use this flag.
- cv2.IMREAD_UNCHANGED: This parameter takes an RGB image from the source and returns the same RGB image as the output. Similarly, if the image is ARGB and has an additional transparency channel, then it will load the image in ARGB mode. The integer value “-1” can be passed to the cv2.imread() function to use this flag.
Note: In Python, a function named “cv2.imshow()” is used to display the read image on the screen.
The following image is used in all the upcoming examples:
The path of this image is shown below:
Default Reading of Image Using cv2.imread()
In the example given below, the default value of the parameter is used to read the image in the colored format using the “cv2.imread()” function.
Code:
import cv2
Pic_path = r'F:\itslinuxfoss\Documents\itslinuxfoss.png'
img = cv2.imread(Pic_path)
cv2.imshow('image', img)
In the following code:
- The Python OpenCV library is imported in the program.
- The path of the image is defined inside the single quotation marks along with the reading mode value named “r”.
- The “cv2.imread()” takes the path variable as an argument and reads the image.
- To show an image on the console screen, the “cv2.imshow()” is used in the program. This function takes two parameters which will show the image title and the read image.
Output:
The output shows that the image is read using the cv2.imread() function.
Reading the Image in Grayscale Using cv2.imread()
In the below-mentioned example, the “cv2.imread()” function takes the parameter value “0” to return the image in Grayscale mode.
Code:
import cv2
pic_path = r'F:\itslinuxfoss\Documents\itslinuxfoss.png'
image_read = cv2.imread(pic_path, 0)
cv2.imshow('image', image_read)
In the following code, the image and its path is identical with the previous example. The difference is that the function named “cv2.imread()” takes the value “0” as its second parameter to read the image in “Grayscale” mode.
Output:
The output shows the image in grayscale mode.
That’s all from this Python OpenCV guide!
Conclusion
In Python, the “cv2.imread()” function reads the image from the specified path. The image is displayed using another function, “cv2.imshow()”. The “cv2.imread()” takes two parameters: path variable and flag. The default flag value will read the image in RGB color mode, and the “0” value reads the image in Grayscale mode. To read an image according to the source image, the flag value “-1” or “cv2.IMREAD_UNCHANGED” is used in the cv2.imread() function. This article provides a complete overview to read and show images in Python.