Python | cv2.imread() Method

Previous
Next

In this tutorial, we will see how to read an image in python programming language using open-cv which exists as cv2 (computer vision) library in python.

We can use imread() method of cv2 library for reading an image ,so first we have to import cv2 library in the python file using import statement.


Now let’s see the syntax and return value of imread() method, then we will move on the examples.

Syntax

Parameters

You can pass two parameters to imread() method. Among these two, path parameter is mandatory while flag parameter is optional.

  1. path: location of the image in your system in string form.
    here two cases can be possible :

    i) if image is present in the current working directory, then you have to mention only the name of the image with their extension like .jpg,.png etc.

    ii) if image is present at somewhere else in your system not in the current working directory then you have to give complete path, also known as abolute path of the image.

  2. flag: This specifies the way in which the image must be read from the system.

Return value

This method returns the matrix of pixels which represent the given image. Pixel is nothing but the smallest unit of the image.

Flag parameters

Let’s see what value you can pass to flag parameter:

Flag Description
cv2.IMREAD_UNCHANGED This flag is used to return the loaded image as is (with alpha channel, otherwise it gets cropped). . Alternatively, we can pass integer value -1 for this flag.
cv2.IMREAD_GRAYSCALE This flag is used to return the image in grayscale format.Alternatively, we can pass integer value 0 for this flag.
cv2.IMREAD_COLOR This flag is used to return the image in BGR color format. It is the default flag. Alternatively, we can pass integer value 1 for this flag.
cv2.IMREAD_ANYDEPTH This flag is used to return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.Alternatively, we can pass integer value 2 for this flag.
cv2.IMREAD_ANYCOLOR This flag is used to return the image in any possible color format.Alternatively, we can pass integer value 4 for this flag.
cv2.IMREAD_LOAD_GDAL This flag is used the gdal driver for loading the image. Alternatively, we can pass integer value 8 for this flag.
cv2.IMREAD_REDUCED_GRAYSCALE_2 This flag is used to return the image in grayscale format and the image size reduced to 1/2 of the original image size .Alternatively, we can pass integer value 16 for this flag.
cv2.IMREAD_REDUCED_COLOR_2 This flag is used to return the image in BGR color format and the image size reduced to 1/2 of the original image size.Alternatively, we can pass integer value 17 for this flag.
cv2.IMREAD_REDUCED_GRAYSCALE_4 This flag is used to return the image in grayscale format and the image size reduced to 1/4 of the original image size .Alternatively, we can pass integer value 32 for this flag.
cv2.IMREAD_REDUCED_COLOR_4 This flag is used to return the image in BGR color format and the image size reduced to 1/4 of the original image size.Alternatively, we can pass integer value 33 for this flag.
cv2.IMREAD_REDUCED_GRAYSCALE_8 This flag is used to return the image in grayscale format and the image size reduced to 1/8 of the original image size .Alternatively, we can pass integer value 64 for this flag.
cv2.IMREAD_REDUCED_COLOR_8 This flag is used to return the image in BGR color format and the image size reduced to 1/8 of the original image size.Alternatively, we can pass integer value 65 for this flag.

cv2.imread() Method example

Now Let’s see the Python code :

Example 1: Read an image in Default mode:

Output :

Output1

Example 2: Read an image in Grayscale mode:

Output :
Output2
Example 3: Read an image and print its dimensions.

(500, 300, 4)

image.shape returns tuple containing (height, width, number of channels)
So in preceding output, 500 pixels is height, 300 pixels is width and 4 is number of channels.
Example 4: Read an image with transparency channel
We can use cv2.IMREAD_UNCHANGED to read transparency channel in image if present.

(500, 300, 4)

Color Channel

imread() takes the image as input and decodes into a matrix with the color channels stored in the order of Blue, Green, Red, and A respectively.
image[:,:,0] represents Blue channel
image[:,:,1] represents Green channel
image[:,:,2] represents Red channel
image[:,:,3] represents Transparency channel

You can write these channels using imwrite() and checkout the differences.

That’s all about cv2.imread() Method in python.

Previous
Next

Add Comment