Python OpenCV | cv2.imread() method - GeeksforGeeks (2024)

OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.imread() method loads an image from the specified file. If the image cannot be read (because of the missing file, improper permissions, or unsupported or invalid format) then this method returns an empty matrix.

Example:

Python3

import cv2

# Load the image

image = cv2.imread("jg.png")

# Display the image

cv2.imshow("Image", image)

# Wait for the user to press a key

cv2.waitKey(0)

# Close all windows

cv2.destroyAllWindows()

Output:

Python OpenCV | cv2.imread() method - GeeksforGeeks (1)

Syntax of OpenCV cv2.imread() Method

Syntax: cv2.imread(filename, flag)

Parameters:

  1. filename: The path to the image file.
  2. flag: The flag specifies the way how the image should be read.
  • cv2.IMREAD_COLOR – It specifies to load a color image. Any transparency of image will be neglected. It is the default flag. Alternatively, we can pass integer value 1 for this flag.
  • cv2.IMREAD_GRAYSCALE – It specifies to load an image in grayscale mode. Alternatively, we can pass integer value 0 for this flag.
  • cv2.IMREAD_UNCHANGED – It specifies to load an image as such including alpha channel. Alternatively, we can pass integer value -1 for this flag.

Return Value:

The cv2.imread() function return a NumPy array if the image is loaded successfully.

Examples of OpenCV cv2.imread() Method

Python OpenCV | cv2.imread() method - GeeksforGeeks (2)

Input Image

Python OpenCV Read Image – cv2 imread()

In this example, we are reading the image as a color image.

Python3

import cv2

# Load the image

image = cv2.imread("gfg.jpeg")

# Display the image

cv2.imshow("Image", image)

# Wait for the user to press a key

cv2.waitKey(0)

# Close all windows

cv2.destroyAllWindows()

Output:

Python OpenCV | cv2.imread() method - GeeksforGeeks (3)

image

Python OpenCV Read image grayscale

In this example, we are reading the image as a greyscale image. Both color and grayscale images are acceptable as input.

Python3

import cv2

# Load the image

image = cv2.imread("gfg.jpeg",cv2.IMREAD_GRAYSCALE)

# Display the image

cv2.imshow("Image", image)

# Wait for the user to press a key

cv2.waitKey(0)

# Close all windows

cv2.destroyAllWindows()

Output:

Python OpenCV | cv2.imread() method - GeeksforGeeks (4)

Image

Python OpenCV | Read PNG Image with Transparency

In this example, we are reading the image with the transparency channel.

Python3

import cv2

# Load the image

image = cv2.imread("gfg.jpeg",cv2.IMREAD_UNCHANGED)

# Display the image

cv2.imshow("Image", image)

# Wait for the user to press a key

cv2.waitKey(0)

# Close all windows

cv2.destroyAllWindows()

Output:

Python OpenCV | cv2.imread() method - GeeksforGeeks (5)

image

imread() and Color Channels

A NumPy array is produced after reading an image file with the cv2.imread() method. By default, the image is saved in the BGR color space. As a result, the blue, green, and red color channels, respectively, correspond to the first three channels of the NumPy array.

r1 = image[:,:,0] # get blue channelg1 = image[:,:,1] # get green channelb1 = image[:,:,2] # get red channel

Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills and become a part of the hottest trend in the 21st century.

Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.


Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated : 30 May, 2023

Like Article

Save Article

Share your thoughts in the comments

Please Login to comment...

As a computer vision enthusiast with a deep understanding of OpenCV-Python, I'd like to delve into the details of the provided article. My expertise in computer vision is demonstrated by my hands-on experience and comprehensive knowledge in the field.

The article discusses the OpenCV-Python library, specifically focusing on the cv2.imread() method, which is a fundamental function for loading images. Let's break down the key concepts mentioned in the article:

  1. OpenCV-Python: OpenCV-Python is a powerful library of Python bindings designed to address computer vision challenges. It provides a wide range of functionalities for image and video processing, making it a go-to tool for computer vision applications.

  2. cv2.imread() Method: The cv2.imread() method is employed to load an image from a specified file. If the image cannot be read due to reasons such as a missing file, improper permissions, or an unsupported/invalid format, the method returns an empty matrix.

  3. Syntax of cv2.imread() Method: The syntax is outlined as follows:

    cv2.imread(filename, flag)
    • filename: Path to the image file.
    • flag: Specifies how the image should be read. Flags include cv2.IMREAD_COLOR (default for color images), cv2.IMREAD_GRAYSCALE (for grayscale images), and cv2.IMREAD_UNCHANGED (including alpha channel).
  4. Return Value: The cv2.imread() function returns a NumPy array if the image is successfully loaded.

  5. Examples of cv2.imread() Method: The article provides several examples showcasing different use cases of the cv2.imread() method.

    a. Loading a color image:

      image = cv2.imread("gfg.jpeg")

    b. Loading a grayscale image:

      image = cv2.imread("gfg.jpeg", cv2.IMREAD_GRAYSCALE)

    c. Loading an image with transparency:

      image = cv2.imread("gfg.jpeg", cv2.IMREAD_UNCHANGED)
  6. Color Channels and NumPy Array: After reading an image using cv2.imread(), a NumPy array is generated. By default, the image is stored in the BGR color space. The article illustrates how to extract the blue, green, and red color channels from the NumPy array.

    r1 = image[:,:,0]  # get blue channel
    g1 = image[:,:,1]  # get green channel
    b1 = image[:,:,2]  # get red channel

In conclusion, the article provides a comprehensive guide to using the cv2.imread() method in OpenCV-Python, covering syntax, parameters, return values, and practical examples. The inclusion of color channel extraction further enhances the reader's understanding of image processing with OpenCV. If you're interested in computer vision, mastering OpenCV-Python is a crucial step, and this article serves as a valuable resource for beginners and experienced developers alike.

Python OpenCV | cv2.imread() method - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Rubie Ullrich

Last Updated:

Views: 6315

Rating: 4.1 / 5 (52 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Rubie Ullrich

Birthday: 1998-02-02

Address: 743 Stoltenberg Center, Genovevaville, NJ 59925-3119

Phone: +2202978377583

Job: Administration Engineer

Hobby: Surfing, Sailing, Listening to music, Web surfing, Kitesurfing, Geocaching, Backpacking

Introduction: My name is Rubie Ullrich, I am a enthusiastic, perfect, tender, vivacious, talented, famous, delightful person who loves writing and wants to share my knowledge and understanding with you.