Open In App

Reading and Displaying Images in Python using OpenCV

Last Updated : 11 Nov, 2025
Comments
Improve
Suggest changes
86 Likes
Like
Report

OpenCV provides functions like cv2.imread() and cv2.imshow() that make it easy to load images from files and display them in a window. It supports common image formats such as JPEG, PNG, BMP, TIFF, WebP and others supported by GDAL.

Syntax

cv2.imread(path, flag)

Parameters:

  • path: String path to the image file.
  • flag: Mode in which image is read.

Return Value: An image object (NumPy array).

Image Reading Flags

OpenCV provides three main flags for reading images using cv2.imread():

  • cv2.IMREAD_COLOR (1): Loads a color image. Any transparency in the original image is ignored. This is the default flag.
  • cv2.IMREAD_GRAYSCALE (0): Loads the image in grayscale mode, where each pixel represents the intensity of light.
  • cv2.IMREAD_UNCHANGED (-1): Loads the image as it is, including the alpha (transparency) channel if present.

Note: The image should be in the working directory or a full path of image should be given. By default, OpenCV stores colored images in BGR(Blue Green and Red) format.

Example

Input Image:

images.png
Python
import cv2

img = cv2.imread("images.png", cv2.IMREAD_COLOR)
cv2.imshow("image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

11_compressed
Output

Explanation:

  • cv2.imread(path, flag): Reads the image. Here, IMREAD_COLOR loads it in color.
  • cv2.imshow(window_name, img): Creates a GUI window and shows the image.
  • cv2.waitKey(0): Keeps the window open until a key is pressed.
  • cv2.destroyAllWindows(): Closes all windows.

Displaying Image with Matplotlib

Matplotlib can also be used to display images in Python. It is especially useful when working in Jupyter notebooks or when you want to visualize images alongside plots and data.

Python
import cv2
import matplotlib.pyplot as plt

img = cv2.imread("images.png")

plt.imshow(img)
plt.waitforbuttonpress()
plt.close('all')
Image read using cv2 and displaying using matplotlib will display picture in BGR format.

Explanation:

  • cv2.imread("images.png"): Reads the image in BGR format.
  • plt.imshow(img): Displays the image.
  • plt.waitforbuttonpress(): Waits for a key or mouse click before closing.

Note: See the difference in colors of images read by cv2 and matplotlib library. Because cv2 uses BGR color format and matplotlib uses RGB color format.

Fixing BGR to RGB Conversion

OpenCV reads images in BGR format, while Matplotlib displays them in RGB. Converting the image from BGR to RGB ensures correct color representation.

Python
import cv2
import matplotlib.pyplot as plt

img = cv2.imread("images.png")

RGB_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

plt.imshow(RGB_img)
plt.waitforbuttonpress()
plt.close('all')

Output

BGR color format changes to RGB color format

Explanation:

  • cv2.cvtColor(img, cv2.COLOR_BGR2RGB): Converts BGR to RGB.
  • plt.imshow(RGB_img): Displays the color-corrected image.

Reading Image in Grayscale

Grayscale images display only shades of gray. This is useful when color is not required, such as in edge detection or basic image analysis.

Python
import cv2 

path = r'images.png'
img = cv2.imread(path, cv2.IMREAD_GRAYSCALE) 

cv2.imshow('image', img) 
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

Explanation:

  • cv2.imread(path, cv2.IMREAD_GRAYSCALE): reads the image in grayscale.
  • cv2.imshow('image', img): opens a window to display the image.
  • cv2.waitKey(0): waits indefinitely for a key press.

Explore