Read Image using cv2.imread() — OpenCV Python — Idiot Developer

Nikhil Tomar
Analytics Vidhya
Published in
3 min readJun 3, 2021

In this tutorial, we are going to focus on reading an image using the Python programming language. For this, we are going to use the OpenCV library. OpenCV refers to Open Source Computer Vision library aimed at computer vision and machine learning.

To use OpenCV in Python install the following libraries:

  1. NumPy
  2. OpenCV

To install the above libraries, use the following command.

pip install opencv-python 
pip install numpy

Import

Here, we are going to import all the required libraries.

import numpy as np 
import cv2

cv2.imread — Reading an Image

To read an image cv2.imread() function is used.

Syntax: cv2.imread(path, flag)

  • path: The path represents the location of the image on the disk.
  • flag: The flag represents the way in which the image should be read. The default value is cv2.IMREAD_COLOR.

If the path is correct then an image matrix is returned, else nothing (None) is returned.

There are three ways in which an image can be read. We can use the following value for the flag parameters in the cv2.imread() function.

  • cv2.IMREAD_COLOR: It is used to read the image as an RGB image. It ignores the alpha channel present in the image. It is the default value for the flag parameters. You can also specify 1 for this flag.
  • cv2.IMREAD_GRAYSCALE: It is used to read the image as grayscale i.e., black and white format. You can also specify 0 for this flag.
  • cv2.IMREAD_UNCHANGED: It is used to read the image as it is. It does not make any changes or ignore anything from the image. You can also specify -1 for this flag.

Reading Image in RGB Format

""" Image path representing the location of the image on the disk. """ 
image_path = "python-logo.png"
""" Reading the image and returning the image matrix """
image = cv2.imread(image_path, cv2.IMREAD_COLOR)

The image variable represents the image matrix in the form of numpy.ndarray class. To get the shape of this numpy.ndarray use the shape function.

print(image.shape) print(f"Height:\t\t {image.shape[0]}") 
print(f"Width:\t\t {image.shape[1]}")
print(f"Channel:\t {image.shape[2]}")
Height, width and number of channels of the RGB image.

The statement image.shape returns a list with three values representing the height, width and number of channels.

""" Displays the image in a GUI window. Press ESC key and the window is removed automatically. """
cv2.imshow("Python Logo", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Image is displayed in a GUI window in a RGB format.

Reading Image in Grayscale Format

import numpy as np 
import cv2
""" Image path representing the location of the image on the disk. """
image_path = "python-logo.png"
""" Reading the image and returning the image matrix """
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
print(image.shape) print(f"Height:\t\t {image.shape[0]}")
print(f"Width:\t\t {image.shape[1]}")
Height and width of the grayscale image.
""" Displays the image in a GUI window. Press ESC key and the window is removed automatically. """ 
cv2.imshow("Python Logo", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Image is displayed in a GUI window in a grayscale format.

Read More

Originally published at https://idiotdeveloper.com on June 3, 2021.

--

--