This website is made possible by displaying online advertisements to our visitors.
Please consider supporting us by disabling your ad blocker.

How to Read, Write, and Save Images with OpenCV and Python

Nov. 19 2021 Yacine Rouizi
OpenCV Image Processing
How to Read, Write, and Save Images with OpenCV  and Python

In this tutorial, I will show you 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

Sponsored

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.

Sponsored

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.

If you need help, don't hesitate to leave a comment in the section below.

Sponsored

Support DontRepeatYourself

If you appreciate what I am doing here, or if it helped you solve your issues please consider buying me a coffee (or 2) as a token of appreciation. It will mean a lot to me and it will really make a difference.

Thank you for your support.

Buy Me a Coffee at ko-fi.com

Previous Article
Transfer Learning with Keras, TensorFlow, and Python

Transfer Learning with Keras, TensorFlow, and Python

Next Article
How to Read and Write Videos with OpenCV  and Python

How to Read and Write Videos with OpenCV and Python

Join the mailing list to be notified about new posts and updates.

Leave a comment

(Your email address will not be published)