Python | cv2.imread() Method

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.
[toc]
Now let’s see the syntax and return value of imread() method, then we will move on the examples.

Syntax

cv2.imread(path ,flag)

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:
[table id=25 /]

cv2.imread() Method example

Now Let’s see the Python code :

Example 1: Read an image in Default mode:

# import computer vision library(cv2) in this code
import cv2

# main code
if __name__ == "__main__" :

    # mentioning absolute path of the image
    img_path = "C:\\Users\\user\\Desktop\\flower.jpg"

    # using imread() method of cv2
    image = cv2.imread(img_path)

    # show the image on the newly created image window
    cv2.imshow('image window',image)

Output :

Output1

Example 2: Read an image in Grayscale mode:

# import computer vision library(cv2) in this code
import cv2

# main code
if __name__ == "__main__" :

    # mentioning absolute path of the image
    img_path = "C:\\Users\\user\\Desktop\\flower.jpg"

    # using imread() method of cv2
    image = cv2.imread(img_path,cv2.IMREAD_GRAYSCALE)

    # show the image on the newly created image window
    cv2.imshow('image window',image)

Output :

Output2

Example 3: Read an image and print its dimensions.

# import computer vision library(cv2) in this code
import cv2

# main code
if __name__ == "__main__" :

    # mentioning absolute path of the image
    img_path = "C:\\Users\\user\\Desktop\\flower.jpg"

    # using imread() method of cv2
    image = cv2.imread(img_path,cv2.IMREAD_GRAYSCALE)

    # print image dimensions
    print('Image Dimensions :', image.shape)
(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.

# import computer vision library(cv2) in this code
import cv2

# main code
if __name__ == "__main__" :

    # mentioning absolute path of the image
    img_path = "C:\\Users\\user\\Desktop\\flower.jpg"

    # using imread() method of cv2
    image = cv2.imread(img_path,cv2.IMREAD_UNCHANGED)

    # print image dimensions
    print('Image Dimensions :', image.shape)
(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.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.