In this tutorial, you will learn how to read, write, and show images using the OpenCV library. To doing this we will use cv2.imread(), cv2.imread() and cv2.imshow() functions. Also, you will learn how to create an image using python NumPy library and how to display images using the python Matplotlib library.

OpenCV provides the cv2.imread() function to read images from a file or url and the cv2.imwrite() function to write images to a file and the cv2.imshow() function to display images on the screen. These functions support various image files like BMP, JPEG, PNG, and TIFF.

Create an Image Using Numpy

Firstly, we will create our image file using Python NumPy library. The image file that we will create is a multidimensional array. It has columns and rows of pixels. Each pixel has a value. The following images represent image pixel values. Normally, this image consists of blue, green, and red colors. I converted the image bgr(blue-green-red) color to grayscale color. The second image represents the grayscale values. Also, you can change the pixel values by accessing the image pixels. In this tutorial, we will see how to change our image pixel values. Now, we will create an image with the NumPy library.

opencv logo
opencv logo pixel values

NumPy library is python’s mathematical library. It helps us to calculate various mathematics operations. And in this tutorial, we will use NumPy to matrices. Because images consist with matrices.

import numpy as np
image = np.zeros((10, 10), dtype=np.uint8)
# image dimension
image.shape

Output

(10, 10)

image
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)

All pixels of this image consists of zeros. If you display this image on the screen you will see a black screen. Because images take pixel values to arrange 0-255. 0 is black, 255 is white and in-bettween shades of gray. Let’s see our image on the screen. We need to python matplotlib library to display images on the screen. Run the following command to show the image.

import matplotlib.pyplot as plt
plt.imshow(image)
matplotlib

You can change the image pixel value with the following code.

image[5,5] = 150
image
array([[  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [  0,   0,   0,   0,   0, 150,   0,   0,   0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0]], dtype=uint8)
matplotlib

Using OpenCV

You can manipulate image pixels using OpenCV easily. Let’s see how you use OpenCV with images. Run the following code to import the OpenCV library.

import cv2

Read an Image

You can use the function cv2.imread() to read images. The image should be in the working directory or a full path of image should be given.

The second argument of the cv2.imread() function is a flag to specify an image color format. For instance, bgr color or grayscale.

  • cv2.IMREAD_COLOR : It loads a color image. It’s also a default flag so, you don’t need to define this argument.
  • cv2.IMREAD_GRAYSCALE : It loads a grayscale image.
  • cv2.IMREAD_UNCHANGED : It loads the image with alpha channel. Alpha channel represents the image transparency.

Note : You don’t need to define all of them. You can define like this:

cv2.IMREAD_COLOR instead 1

cv2.IMREAD_GRAYSCALE instead 0

cv2.IMREAD_unchanged instead -1

You can define it like the following command.

image = cv2.imread("my_image.jpg" cv2.IMREAD_COLOR)
image = cv2.imread("my_image.jpg" 1)

Run the following code to read an image from a file or path.

import numpy as np
import cv2
# Load an color image
image_color = cv2.imread('opencv_logo.jpg')
# or
image_color = cv2.imread("opencv_logo.jpg",1)
# Load an image in grayscale
image_grayscale = cv2.imread("opencv_logo",0)
# Load an image in alpha
image_alpha = cv2.imread("opencv_logo",-1)

Display an Image


You can use the cv2.imshow() function to display an image on the screen. The first argument is the name of the window and the second argument is our image.

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

When you run the above code, you will get an image like the image below.


cv2.waitKey() is a keyboard binding function. It just takes one argument and the argument is a time in milliseconds. If you type 0 or don’t type anything, it will wait forever. But if you type 10000, it will wait 10 seconds.

# to wait forever
cv2.waitKey()
# or
cv2.waitKey(0)

cv2.destroyAllWindows() destroys all windows we created. Also It don’t get any argument.

# to wait 10 seconds
cv2.waitKey(10000)
# to wait 1 seconds
cv2.waitkey(1000)

Write an Image


You can use the function cv2.imwrite() to save an image. The first argument is the name or path and the second argument is our image. If you want to save the image in the working directory, you only have to type the image name. However, if you want to save the image in any directory, you must type the image name with the path.

# to save in working directory.
cv2.imwrite('opencv_logo.png',image)
# to save in any directory.
cv2.imwrite('C:/Users/rosdeepy/opencv_logo.png',image)
print("hello world")