Loading, Displaying & Saving images — OpenCV

Written by: Chirag (Srce Cde)


Today, we deal with digital data in every aspect including text, images, audio, videos and so on. Here, we are going to take image data into consideration for the purpose of this article. With image data we can perform multiple operations upon them like applying filters, editing, image translations and many more. But, to perform any operation on the data the very first aspect is to load the data, correct?

In this article we will learn…

  • How to load images from disk
  • Display images
  • Save images back to disk

Loading image

Let’s get started and go ahead and create image_processing_opencv.py

# import required packages
import cv2

The very first thing is to import the necessary packages and in our case its cv2 (It contains the image processing functions that we have used in the above snippet)

OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library.


# loading image
img = cv2.imread('path/of/image/file.jpg')

Since, we have imported the required packages now we are good to load the image. For loading the image from disk we will use cv2.imread function. cv2.imread will return the NumPy array of the image. To load the particular image, we need to pass the image path as a parameter to that function. Here, we are passing the image path as static but one can also pass it as a command line argument. Finally, we will have the numpy array of the image within img variable. Hence, this is how we can load the image from the disk using OpenCV.

Since, we have the image we can perform number of operations. For example you want to change the color space of the image to grayscale or HSV. For that you can use cv2.cvtColor function.


# converting to grayscale
gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# converting to HSV colorspace
hsv_img = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)

Display image


# displaying image
cv2.imshow('Image file', img)
cv2.waitKey(0)

As a second step, we will display the image. Here, we will use cv2.imshow function. cv2.imshow accepts two parameters. First parameter is basically a string and it’s the title of the display window. The second parameter is the actual image/numpy array, which is img in our case that we want to display. Then we have cv2.waitKey which basically pause the execution of script until the user press any key from keyboard. And we have passed 0 as a parameter which indicates that when the user press any key then the execution will resume. The image window will look something like this.

image preview opencv
Image credits: unsplash.com

Saving image


# saving file to disk
cv2.imwrite("path/to/save/newfile.jpg", img)

Finally, we want to save the image. To save the image we are going to use cv2.imwrite function, which accepts two parameters. The first parameter is the path of the image, where you want to save the image. In my case, I want to save the image as newfile.jpg. And the second parameter is the actual image or the numpy array that we want to save.

Also, note that if you want to save the image in any other format then the conversion part is handled by OpenCV automatically.

Now, go ahead and open the terminal, and run python3 image_processing_opencv.py This will load the image, display it and save it on the given path.

Conclusion

In this article, we learned that how to load the image, how to display it and save it on the provided path using OpenCV library. Please stay tuned for more articles going forward.