Fundamental OpenCV Functions: Loading, Displaying and Saving Images

Samuel Ozechi
3 min readAug 20, 2023

Loading, displaying and saving images are some of the most basic and
commonest uses of the OpenCV library. You would often require to load
images before you can work with them. Displaying images is useful in
debugging computer vision projects as it is also important to save work
done on images.

In this article, we’ll delve into the steps of image handling. Follow along as we explore image loading, extracting vital dimensions like height, width, and channels, visualize images on screen, and saving modifications. We would use a sample image of the Eiffel Tower (saved as eiffel_tower.jpg in my working directory).

Loading Images
1. If you don’t have OpenCV installed already, you can do so by using !pip install opencv for Jupyter Notebook or pip install py-opencv for PyCharm terminal.
2. Import the OpenCV package by including import cv2 in your code.
3. Load the image using the cv2.imread function. Provide the path to the image as an argument, or simply the image filename if it’s in the same directory as your Python file.

# import the required packages
import cv2

# load the image from disk
image = cv2.imread('eiffel_tower.jpg')

The cv2.imread function accepts a string argument specifying the image path. In this instance, I provided just the filename (eiffel_tower.jpg), as the image resides in the same directory as my Python file. Additionally, the cv2.imread function offers versatility, allowing images to be loaded in color using the flag cv2.IMREAD_COLOR, as grayscale with cv2.IMREAD_GRAYSCALE, or in their original form with cv2.IMREAD_UNCHANGED.

Getting dimensions
OpenCV loads the image as pixels in a 2 dimensional Numpy array, therefore, the read image possess the shape attribute of Numpy arrays
which returns the spatial dimensions of the image (height, width and
channels respectively).

print("The image is loaded as: ", type(image))
# get the spatial dimensions
# including width, height, and number of channels
(height, width, channels) = image.shape[:3]

# display the image width, height, and number of channels
print("height: {} pixels".format(height))
print("width: {} pixels".format(width))
print("channels: {}".format(channels))

Displaying Image

The loaded image can be displayed using the cv2.imshow function which displays the image in a new window.The function takes a string argument of a window name for the display and the object name of the loaded image. It opens a new window of the image dimension that displays the image.

The displayed window ‘disappears’ almost immediately after being displayed so the cv2.waitKey function is used to keep the window displayed. It takes an argument of how long the window should be displayed in seconds, zero (0) is passed to automatically close the window when a key is pressed. Note that OpenCV require images to be read (using cv2.imread) before they can be displayed.

# show the image and wait for a keypress to quit display
cv2.imshow("Image", image)
cv2.waitKey(0)

Saving Image:

Processed Images can be saved using the cv2.imwrite function.It takes a string argument of the file name to save the image with and the object name of the image to be saved. The string argument can also be used to specify the the path for the new image, if it wont be saved in the working directory.

# save the image back to disk
# with desired filetype and directory
cv2.imwrite("newimage.jpg", image)

I provide a filename argument that results in the file being saved within a directory named new_folder and image named as newimage.jpg. It’s worth noting that OpenCV seamlessly deduces the file format for the saved image (in our case, JPEG) based on the filename. To ascertain the array of currently supported file formats, consult the OpenCV documentation.

Summary

I described the steps required to load, display and save images with OpenCV and the respective functions for such operations. I also described how to get the spatial dimensions of images.

--

--