Read Display and Save Images

Download the source code

NOTE: Please reload the page when you jump from one lecture to another so that the code highlighter is loaded! Sorry for the inconvenience.

In this part, you'll learn how to read, display, and write images using OpenCV's built-in functions.

We will use:

The image below will be used as an example throughout this tutorial:

Sample image

Read an Image (cv2.imread)

To read an image with OpenCV, we can use the cv2.imread(filename, flags) function. It takes 2 arguments:

  • The first one is the path to the image to be loaded.
  • The second argument, which is optional, is a flag that lets you specify the reading mode. There are several options for this flag which you can find in the documentation but the default value is cv2.IMREAD_UNCHANGED or -1 which returns the loaded image as is.

The imread function returns a Numpy array that represents the image or None if the image couldn't be loaded.

Here is the syntax:

import cv2 

image = cv2.imread("path-to-image.jpg")

For your reference, check out the documentation for the imread() function.

Display an Image (cv2.imshow)

To display an image with OpenCV, we use the imshow(win_name, img) function. This function also takes 2 arguments:

  • The first argument is the name of the window on which the image will be displayed.
  • The second argument is the image to display.

In order to display the image, we need to call another function: cv2.waitKey(). This function is responsible for keeping the window open for some time. We can pass it a number to specify how long (in milliseconds) the window should stay open. 0 instructs the function to keep the window open indefinitely until a key is typed.

let's see some examples of reading and displaying an image.:

image = cv2.imread('image.jpg')
cv2.imshow('color image', image)
cv2.waitKey(0)

Color image

image = cv2.imread('image.jpg', cv2.IMREAD_UNCHANGED)
cv2.imshow('unchanged image', image)
cv2.waitKey(0)

Unchanged image

image = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
cv2.imshow('grayscale image', image)
cv2.waitKey(0)

Grayscale image

For your reference, check out the documentation for the imshow() function.

Write an Image (cv2.imwrite)

To save an image into the file directory, we use the imwrite(filename, img) function. The function takes 2 arguments.

  • The first argument is the file name. The function chooses the image format from the file name extension.
  • The second argument is the image we want to save.

The function returns a Boolean: True if the image was saved successfully or False otherwise.

image = cv2.imread("cards.jpg") # read the image
cv2.imwrite("newimage.png", image) # save image

If you check the content of the directory you will see that a file newimage.png was added:

$ ls
image.jpg  newimage.png  read_write_save.py

For your reference, check out the documentation for the imwrite() function.

Summary

With these built-in functions, you can now easily load, display, and save images using OpenCV.

Try experimenting with these functions and if you are a beginner I recommend that you write the code by hand (do not copy/paste) to familiarize yourself with the library.

Discussion

0 comments