Introduction to OpenCV imread

The following article provides an outline for OpenCV imread. OpenCV is a technique or library which enables a user to process images and supports users to solve problems related to Computer Vision. It provides a number of functions which include imread. The imread function helps in loading an image from a specified folder. In order to process an image this image has to be read first. The job of this function is to return an empty matrix if it finds a missing file or any improper permissions, invalid formats or unsupported file formats. This will indicate that there is an issue with the image which is to be processed.


cv2.imread(path, flag)


  • path: This will be a string which will let the compiler know where the image is.
  • flag: It will specify a way in which the image is supposed to be read. By default, it takes a colour value which is cv2.IMREAD_COLOR.

The return value of this function will be an image which will be loaded from the path and file which was specified earlier.

How does imread Function Work?

The imread function is a function of the OpenCV library. OpenCV is used for deep learning algorithms. It will read the image for further processing which will help in classification and analysis purposes. In order to use these images, the first step will be reading an image. This is performed by using this function.

The working of this function is as below:

  • imread: This will read the image as it is from the specified path and load it.
  • imread_multi: There can be cases when multiple images have to be loaded. The imread_multi function helps in loading multiple images. These images can be of TIFF format or ATK format. The sub based format images of TIFF can also be read using this function.
  • imsave: This option helps in saving and writing an image wherever required.

The images can be read in different formats as well. The second argument in the function call works in the way you want the image to be processed. There are three ways of processing an image.

One of the ways was mentioned above where you can load the image in coloured format.

  • Cv2.imread_color: The image in this format will be loaded in a coloured format. If there is transparency in the image, then it will be getting ignored and the default flag will be set.
  • Cv2.imread_grayscale: In order to process and analyse an image is mostly converted to grayscale image so that it can be classified and algorithms can be applied. This function creates the image in a grayscale mode.
  • Cv2.imread_unchanged: This mode helps in loading the image using an alpha channel.

These are the three modes which help in reading and processing the image simultaneously. Instead of using these lengthy names for the functions you can use a short way of just passing three flags which are represented by using integers. We can pass the argument as 1 when you want the colored image and 0 when it is to be grayscale and -1 when it ought to be unchanged.

Let us now have a look at an example and try to understand the functionality.


Python program to understand imread function.


# import library cv2
import cv2
#Store path where the image is in a variable
p = r'C:\Users\aanch\eduCBA.JPG'
# Use cv2.imread() method in OpenCV to read and load the image
imgr = cv2.imread(p)
# Display the image that is loaded
cv2.imshow('image', imgr)


OpenCV imread op 1

Let us go through the code and understand what it is doing here.

We have first imported the cv2 library. If you do not have the cv2 library inserted, you can install it using pip install command to install the opencv-python library which has the required methods. We then take a variable ‘p’ where we will read the image which is present at a particular path. We specify the path here and store it in variable p. We then use the imread() function in this library which helps us in reading the image at this path. We did not pass any second argument hence the image will be loaded as it is without any changes. After reading the image we will have to display it in order to check if it has been read properly. To display the image we use the imshow() function present in the same library. We pass the argument of ‘imgr’ and then enter the waitKey() method to let the window having the image stay and then destroy this window from preventing it from running in the background. A new window with the image will be displayed. If you observe the above output, you will see the image which will be displayed.

Another variant here is displaying an image in grayscale. You can use the IMREAD_GRAYSCALE() method in the second argument to change the image to grayscale. Below code will help in having a black and white image.


import cv2
# Use cv2.imread() method in OpenCV to read and pass GRAYSCALE to load it in grayscale
p_gray = cv2.imread('eduCBA.JPG', cv2.IMREAD_GRAYSCALE)
# Display the image that is loaded
cv2.imshow('image_gray', p_gray)

The only change in the above code that you will observe is that we use the imread function with a second argument of cv2.IMREAD_GRAYSCALE which helps us changing the image passed to the imread function to grayscale.


OpenCV imread op 2

You can see the image is not in greyscale.

Conclusion – OpenCV imread

OpenCV is a library which helps us in processing images for deep learning. In order to analyse and obtain concrete results we need to first read the image and only then you can process it further. The imread() function is used for reading the image and further it can be loaded. The function helps us in reading the image as it is or for converting it to grayscale or even reading multiple images at the same time.

Recommended Articles

This is a guide to OpenCV imread. Here we discuss the introduction and how does imread function work? for better understanding. You may also have a look at the following articles to learn more –

  1. OpenCV kmeans
  2. Python AssertionError
  3. Python Array Length
  4. Python IndexError

The post OpenCV imread appeared first on EDUCBA.