Read an Image in OpenCV ( Python, C++ )

By | April 7, 2015

In OpenCV you can easily read in images with different file formats (JPG, PNG, TIFF etc.) using imread. The basic usage is shown below


Mat imread(const string& filename, int flags=IMREAD_COLOR )


image = cv2.imread(filename, flags=cv2.IMREAD_COLOR)

The flags option is used to control how the image is read. Let’s look at some common examples. In all the examples below, make sure you use the right namespace for C++ and import OpenCV for Python.

using namespace cv; 


import cv2

Read as 8-bit / channel Color Image ( without Alpha Channel)

A vast majority of images are 8-bit per channel ( or 24-bit ) images. They can be read using default flags.


Mat image = imread("image.jpg");


image = cv2.imread("image.jpg")

Read as 8-bit Grayscale Image


Mat image = imread("image.jpg", IMREAD_GRAYSCALE);


image = cv2.imread("image.jpg", cv2.IMREAD_GRAYSCALE)

Read 16-bit / channel Color Image

Most digital SLR cameras are capable of recording images at a higher bit depth than 8-bits / channel. The raw images from these cameras can be converted to 16-bit / channel PNG or TIFF images. These 16-bit / channel images can be read using

Mat image = imread("image.png", IMREAD_ANYCOLOR | IMREAD_ANYDEPTH);


im = cv2.imread("image.png", cv2.IMREAD_ANYCOLOR | cv2.IMREAD_ANYDEPTH)

You may also use the flag IMREAD_UNCHANGED instead. See an example below.

Read a Transparent PNG or TIFF in OpenCV

A transparent image has four channels — 3 for color, and one for transparency. These images can be read in OpenCV using the IMREAD_UNCHANGED flag.

Mat image = imread("image.png", IMREAD_UNCHANGED);


im = cv2.imread("image.png", cv2.IMREAD_UNCHANGED)


Download Python / C++ code and example image

You can download a 16-bit transparent PNG and example code from by subscribing to our newsletter. You will also receive a free Computer Vision Resource guide. In our newsletter we share OpenCV tutorials and examples written in C++/Python, and Computer Vision and Machine Learning algorithms and news.

Subscribe Now

  • Lucas


    can you make something simple about stitching more than two images? (not the simple Stitch class)
    I am sorry to send this message through here. I didn’t find your contact.

    findMatches(image1, image2); //find features, matches and returns points1 and points2
    Mat Homog = findHomography(points1, points2, CV_RANSAC);
    Mat result;
    warpPerspective(image1, result, Homog, Size(image1.cols+image2.cols, image1.rows+image2.rows));
    Mat half(result, Rect(0, 0, image2.cols, image2.rows));

    something like this.

    thank you!
    Great website, I already subscribed =)

    • Thanks a bunch for the suggestion. I will put this on my list for sure. My next post will be Part 2 of OpenCV as a web API, but I can write this one after that.

      • Lucas Ribeiro de Abreu

        thank you, OpenCV as a web API is also very interesting! Do you know any easy way to make an openCV web API for C++ too?

  • Pankaj Randhe

    Hi Satya,

    Can you please suggest which parameter I should use if I want to read Hyperspecral image with 120 bands and is in .TIFF format. I tried all the parameters, but when I check it using img.channels() it is showing 4 bands maximum.

    Thank You.

    • Abhinav Garg

      Hi Pankaj,

      OpenCV is used to read 3 Bands RGB image (meaning 3 channels). Sometimes there may be an alpha channel so RGBA (4 channels) image.

      You can not read 120 bands using opencv function imread. You will have to write your own code to read 120 bands. You can process it as stack of OpenCV Mat containing 120 Matrices.

      I am not sure how can you read .TIFF file using an opensource library. There is multibandread function in Matlab which does that.


  • Saurabh Khanduja

    Hi, There is a recent(few months back, so not so recent) addition of handling of EXIF Orientation metadata. Check this out:

    • Thanks. Very nice. I will ready up on it and add to this post.

    • Nitheesh A S

      Were you able to make it work? I’m not. :/

      • Saurabh Khanduja

        Are you using latest addition? I didnt use it as we are already using imagemagick to auto-orient images before processing(calling imagemagick bash command from java). If interested, install imagemagick and run this command “convert -auto-orient originalImagePath orientedImagePath”

        • Saurabh Khanduja

          However, before moving to imagemagick, I suggest you make sure that opencv source that you are building has these changes. You can do a git pull from and then build it. This will have all the changes. Once built, install exiftool and find images which have orientation 2 to 8.
          Test on these images. Open these images in sublime(yes sublime can open images as well), they will open with orientation not corrected, similar to how opencv used to open it before. But if you open them in any image viewer, these image will have different orientation. Not use opencv and this time image will open with correct orientation if you are using latest repo.

          • Nitheesh A S

            I used exiftags from PIL to find the orientation in python, but I want to make this work in C++. I was using opencv v3.0 only. I’ll clone the latest and try this out! 🙂

  • Dr Watson

    Check this out:
    opencv C++ Tutorials available with full source code !