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.

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:

FlagDescription
cv2.IMREAD_UNCHANGEDThis 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_8This 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.

Related Posts

  • Convert image to grayscale in Python
    21 August

    Convert image to grayscale in python

    Table of ContentsUsing the .convert() function from the Pillow Imaging Library (Fork) to convert image to grayscale in Python.Using the OpenCV library functions to convert image to grayscale in Python.Using the color.rgb2gray() function from the scikit-image module to convert image to grayscale in Python.Using the Conversion Formula along with the matplotlib library to convert image […]

  • Rotate image Python
    20 August

    Rotate image in Python

    Table of ContentsUsing the image.rotate() function from the Pillow Imaging Library (Fork) to rotate image in Python.Using NumPy library functions to rotate image in Python.Using the OpenCV and imutils library functions to rotate image in Python.Using matplotlib and SciPy libraries to rotate image in Python. Python supports the use of images and sometimes a given […]

  • 09 August

    Python | Introduction and Installation of OpenCv

    Table of ContentsIntroductionComputer Vision1) Acquiring an image2) Processing the image3) Understanding the imageApplications1) Sports – Hawk Eye2) Smart carsOpenCVInsall openCV on windows and linuxDownloading and Installing OpenCV: In this tutorial, we are learning about what is OpenCv and how OpenCV is installed in windows/linux system. Introduction Have you ever seen Facebook’s auto-tag feature? Whenever you […]

  • 09 August

    Python | cv2 imshow() Method

    Table of ContentsSyntaxParametersReturn ValueReturn value In this tutorial, we will see how to display an image as an output using python by the use of open-cv which is exist as cv2 (computer vision) library. We can use imshow() method of cv2 library to display an image in a window. In order to use cv2 library, […]

  • 09 August

    Python | cv2 imwrite() Method

    Table of ContentsSyntaxParametersReturn Valuecv2 imwrite() method Examples In this tutorial, we will see how to save an image in your own system using python by using open-cv which exists as cv2 (computer vision) library. You can use imwrite() method of cv2 library to save an image on your system. To use cv2 library, you need […]

  • 23 May

    Python | cv2 findContours() Method

    Table of ContentsSyntaxParametersReturn ValueContours Approximation Methodcv2 findContours() Method examplesExample 1: Using cv2.RETR_TREE as a retrieval mode and cv2.CHAIN_APPROX_NONE as a Contour approximation method.Example 2: Using cv2.RETR_EXTERNAL as a Contour retrieval mode and cv2.CHAIN_APPROX_SIMPLE as a Contour approximation method.Reference In this tutorial, we will see how to find all boundary points(x,y) of an object in the […]

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.