Change language

Python OpenCV | cv2.imread () method

OpenCV-Python — is a Python bindings library for solving computer vision problems. 
cv2.imread () loads an image from the specified file. If the image cannot be read (due to a missing file, incorrect permissions, unsupported or incorrect format) then this method returns an empty matrix.

Syntax: cv2. imread (path, flag)

Parameters:
path: A string representing the path of the image to be read.
flag: It specifies the way in which image should be read. It’s default value is cv2.IMREAD_COLOR

Return Value: This method returns an image that is loaded from the specified file.

Note . The image must be in the working directory or the full path to the image must be specified.

All three types of flags are described below:

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.

Image is used for all examples below:

Example # 1: Using the default flag

# Python program to explain the cv2.imread () method

 
# cv2 import

import cv2

 
# path

path = r ’ C: UsersRajnishDesktoppythonengineering.png’

 
# Using the cv2.imread () method

img = cv2.imread (path)

 
# Display images

cv2.imshow ( ’image’ , img)

Output:

Example # 2:
Loading an image in grayscale

# Python program to explain the cv2.imread () method

 
# cv2 import

import cv2

 
# path

path = r ’C: UsersRajnishDesktoppythonengineering.png ’

  
# Using the cv2.imread () method
# Using 0 to read the image in grayscale

img = cv2.imread (path, 0 )

 
# Display image

cv2.imshow ( ’image’ , img)

Output:

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically