Python imread()- Different ways to Load an Image Using the OpenCV.imread() Method

imread() Method:

Imread python: imread() is one of the OpenCV-Python library’s most useful and often used methods. It is used to load an image from the given file into the Python program. After successfully importing the picture, it returns a numpy.ndarray (NumPy N-dimensional array). When a colorful image is loaded, this numpy.ndarray is a 3-Dimensional array; when a grayscale image is loaded, this numpy.ndarray is a 2-Dimensional array.

OpenCV-Python is a Python binding library for solving computer vision problems.

The cv2 module of the opencv-python library is required to use the Python imread() method.

Before we start with the code, first we should install the opencv-python library into our environment as shown below:

pip install  opencv-python
# Import cv2 module using the import keyword
import cv2

Syntax:

cv2.imread(filename/path, flag)

Parameters

filepath: It is a string that represents the image’s path to be read.

flag: This is optional parameter. It normally accepts one of three values: cv2.IMREAD COLOR, cv2.IMREAD GRAYSCALE, and cv2.IMREAD UNCHANGED. This option, in fact, specifies the mode in which the picture should be read.

Note: The value of this flag parameter is cv2.IMREAD COLOR or 1 by default.

Return Value:

  • If the picture is successfully loaded, the cv2.imread() method returns a numpy.ndarray
  • If the image cannot be read for whatever reason, it returns an empty matrix (Mat::data==NULL) (like missing file, improper permissions, unsupported or invalid format).

Python’s imread() function supports the below image formats:

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

Reading images in.JPEG format is dependent on the version of the OpenCV library present on the system, platform, or environment (such as x86/ARM), and so on. The most important thing to remember is that the kind of picture is decided by the content of the numpy.ndarray returned by the cv2.imread() method, not by the image file extension.

Let us take the below sample image:

Sample Image for Implementation purpose

1)Load an image using cv2.IMREAD_COLOR

cv2.IMREAD_COLOR: This specifies that a color image should be loaded. Any image transparency will be ignored. It is the default setting. We can also pass the integer value 1 for this flag.

When the flag cv2.IMREAD COLOR is set, the picture is converted to a three-channel BGR color image with no transparency channel before being imported into the program.

Example

Approach:

  • Import cv2 module using the import keyword.
  • Pass the image file path, in cv2.IMREAD_COLOR mode as arguments to the imread() function to load the given image.
  • Store it in a variable.
  • Print the shape(dimensions) of the given image using the shape attribute
  • The Exit of the Program.

Below is the implementation:

# Import cv2 module using the import keyword
import cv2
# Pass the image file path,in cv2.IMREAD_COLOR mode as arguments to the imread() function
# to load the given image.
# Store it in a variable.
gvn_imag = cv2.imread('dhoni.jpg', cv2.IMREAD_COLOR) 
# Print the shape of given image using the shape attribute
print("The shape of the given image = ", gvn_imag.shape)

Output:

The shape of the given image = (194, 259, 3)

Explanation:

Imread python: There are three values in the output tuple. In the given sample image, 194 is the number of rows (height of the image), 259 is the number of columns (width of the image), and 3 is the number of channels.

Because the flag value is cv2.IMREAD COLOR, the loaded image only includes three channels: blue, green, and red.

2)Load an image using cv2.IMREAD_GRAYSCALE

cv2.IMREAD_GRAYSCALE: It instructs the system to load an image in grayscale mode. We can also pass the integer value 0 for this flag.

Example

Approach:

  • Import cv2 module using the import keyword.
  • Pass the image file path, in cv2.IMREAD_GRAYSCALE mode as arguments to the imread() function to load the given image.
  • Store it in a variable.
  • Print the shape(dimensions) of the given image using the shape attribute
  • The Exit of the Program.

Below is the implementation:

# Import cv2 module using the import keyword
import cv2
# Pass the image file path,in cv2.IMREAD_GRAYSCALE mode as arguments to the imread() function
# to load the given image.
# Store it in a variable.
gvn_imag = cv2.imread('dhoni.jpg', cv2.IMREAD_GRAYSCALE) 
# Print the shape of given image using the shape attribute
print("The shape of the given image = ", gvn_imag.shape)

Output:

The shape of the given image = (194, 259)

Explanation:

There are just two values in the output tuple. The number of rows in the sample image is 194, and the number of columns is 259. When the flag value is either 0 or cv2.IMREAD GRAYSCALE, the image will be loaded as a grayscale image regardless of the input sample image given to the cv2.imread() method.

3)Load an image using cv2.IMREAD_UNCHANGED

cv2.IMREAD_UNCHANGED: It specifies that an image with an alpha channel is loaded. Alternatively, we can set this flag to the integer value -1.

Example

Approach:

  • Import cv2 module using the import keyword.
  • Pass the image file path, in cv2.IMREAD_UNCHANGED mode as arguments to the imread() function to load the given image.
  • Store it in a variable.
  • Print the shape(dimensions) of the given image using the shape attribute
  • The Exit of the Program.

Below is the implementation:

# Import cv2 module using the import keyword
import cv2
# Pass the image file path,in cv2.IMREAD_UNCHANGED mode as arguments to the imread() function
# to load the given image.
# Store it in a variable.
gvn_imag = cv2.imread('dhoni.jpg', cv2.IMREAD_UNCHANGED) 
# Print the shape of given image using the shape attribute
print("The shape of the given image = ", gvn_imag.shape)

Output:

The shape of the given image = (194, 259, 3)

Explanation:

There are three values in the output tuple. In the given sample image, 194 is the number of rows (height of the image), 259 is the number of columns (width of the image), and 3 is the number of channels i.e, blue, green, and red.

Note: For some Images, as the flag value is cv2.IMREAD UNCHANGED, the loaded picture has four channels: blue, green, red, and transparency.