Python imread(): Image Loading with OpenCV.imread()

OpenCV Python Imread()

In this tutorial, we will learn how to use imread() method of OpenCV-Python in detail and different ways to load an image using imread() method.

In Python, the imread() method from the OpenCV library allows for versatile image loading. This function accepts two parameters: the image filename (with the full path if it’s not in the working directory) and a flag parameter that can determine how the image is read.

Understanding imread() Function in OpenCV-Python

imread() is one of the most useful and frequently-used methods of the OpenCV-Python library. It is used to load an image in the Python program from the specified file. It returns a numpy.ndarray (NumPy N-dimensional array) after loading the image successfully. This numpy.ndarray is a 3-Dimensional array when the loaded image is a colorful image and a 2-Dimensional array when the loaded image is a grayscale image.

Setting Up OpenCV for Image Loading in Python

In order to use the Python imread() method, we require the cv2 module of the opencv-python library. For that, we have to first install the opencv-python library inside the virtual environment or on the local system and then import the cv2 module in the Python program. Following are the commands to install and import it:

# Installing the opencv-python library
pip install opencv-python
# Importing the cv2 module
import cv2

Syntax Breakdown of imread() Function in OpenCV-Python

Following is the proper syntax of Python imread() method:

cv2.imread(filename, flag)

Parameters: cv2.imread() method takes two parameters. The two parameters are as follows:

  1. filename is the first and the compulsory parameter to be passed and it takes a string value representing the path of the image file (or the image name with extension). NOTE: We have to pass the full path of the image file if is not in the working directory.
  2. flag is the second and the optional parameter to be passed and it usually takes three types of values: cv2.IMREAD_COLOR, cv2.IMREAD_GRAYSCALE, and cv2.IMREAD_UNCHANGED. Actually, this flag defines the mode in which the image should be read. NOTE: By default, the value of this flag parameter is cv2.IMREAD_COLOR or 1.

Return Value: cv2.imread() method returns a numpy.ndarray (NumPy N-dimensional array) if the loading of the image is successful. NOTE: It returns an empty matrix ( Mat::data==NULL ) if the image cannot be read because of any reason (like missing file, improper permissions, unsupported or invalid format).

Supported Image Formats for imread() in OpenCV-Python

Following are the image formats supported by cv2.imread() method:

  • Portable Network Graphics*.png
  • Portable image format*.pbm, *.pgm, *.ppm *.pxm, *.pnm
  • Windows bitmaps*.bmp
  • JPEG files*.jpeg, *.jpg, *.jpe
  • JPEG 2000 files*.jp2
  • WebP*.webp
  • PFM files*.pfm
  • Sun rasters*.sr, *.ras
  • OpenEXR Image files*.exr
  • Radiance HDR*.hdr, *.pic
  • TIFF files*.tiff, *.tif

NOTE: Reading of .JPEG  format images depend on the version of OpenCV library installed on the system, platform, or environment (like x86/ARM), etc. And the most important thing is the type of image is not determined by the image file extension but by the content of the numpy.ndarray returned by the cv2.imread() method.

Let’s implement everything in Python code…

Sample Image
Working with a Sample Image

Loading Images with cv2.IMREAD_COLOR Flag

When flag is passed with the value cv2.IMREAD_COLOR then the image is first converted to the three-channel BGR color image with no transparency channel and then loaded into the program.

It is the default value of the flag parameter. The integer value corresponding to cv2.IMREAD_COLOR is 1. We can also use 1 in place of cv2.IMREAD_COLOR. NOTE: We are using .shape method to access the shape of the image. It returns a tuple of the number of rows, columns, and channels.

img = cv2.imread('sample_image.png', cv2.IMREAD_COLOR) 
print("Shape of the loaded image is", img.shape)

Output:

Shape of the loaded image is (512, 512, 3)

The output tuple has three values 512 is the number of rows (height of the image) in the sample image, 512 is the number of columns (width of the image), and 3 is the number of channels.

Here the loaded image has only three channels Blue Green & Red as the flag value is cv2.IMREAD_COLOR.

The fourth channel that is the transparency or alpha channel is ignored even if it is there in the sample image.

Loading Images with cv2.IMREAD_GRAYSCALE Flag

When the flag is passed with the value cv2.IMREAD_GRAYSCALE then the image is first converted to a single-channel grayscale image and then loaded into the program. The integer value corresponding to cv2.IMREAD_GRAYSCALE is 0 we can also use 0 in place of cv2.IMREAD_GRAYSCALE.

img = cv2.imread('sample_image.png', cv2.IMREAD_GRAYSCALE)
print("Shape of the loaded image is", img.shape)

Output:

Shape of the loaded image is (512, 512)

The output tuple has only two values 512 is the number of rows in the sample image, and 512 is the number of columns. Irrespective of the input sample image passed to the cv2.imread() method the image will be loaded as a grayscale image when the flag value is either 0 or cv2.IMREAD_GRAYSCALE.

Loading Images with cv2.IMREAD_UNCHANGED Flag

When the flag is passed with the value cv2.IMREAD_UNCHANGED then the image is loaded into the program as it is along with the alpha or transparency channel if it’s there. The integer value corresponding to cv2.IMREAD_UNCHANGED is -1 we can also use -1 in place of cv2.IMREAD_UNCHANGED.

img = cv2.imread('sample_image.png', cv2.IMREAD_UNCHANGED)
print("Shape of the loaded image is",img.shape)

Output:

Shape of the loaded image is (512, 512, 4)

The output tuple has three values 512 is the number of rows (height of the image) in the sample image, 512 is the number of columns (width of the image), and 4 is the number of channels.

Here the loaded image has four channels Blue, Green, Red & Transparency as the flag value is cv2.IMREAD_UNCHANGED. The fourth channel that is the transparency or alpha channel will be included if it is there in the sample image.

Wrapping Up and Exploring Further Possibilities

In this tutorial, you have learned the different ways to load an image just by using different values of the flag parameter. Just remember two things you have to pass the full path of the sample image file if you don’t have it in your present working directory and you can also pass the integer value [1, 0, & -1] to the flag parameter corresponding to [cv2.IMREAD_COLOR, cv2.IMREAD_GRAYSCALE, & cv2.IMREAD_UNCHANGED].

Now, it’s your turn to apply this newfound knowledge. How will you use imread() in your next project?