New Tutorials:   KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL  
CLOSE
   OpenCV   Computer Vision  Python  
   Technology    Programming

Load, Display, and Write Images using OpenCV

         
 JULY 16, 2020   by shekharpandey

The main objective of this tutorial is to understand how to load and display an image in OpenCV using Python. Later on this, we will see first with the help of the python and then with the help of the C++.

After going through this tutorial, you will be able to do the following:

  1. You can load the image using the OpenCV library (using cv2.imread method)

  2. You will be able to display the image on the screen using the cv2.load method.

  3. You will be able to write the image back to your file system using the using cv2.imwrite method.

Apart from this, we can also load the image with the help of the user command-line argument. But that we will discuss later in the other tutorial. In this tutorial, we will focus on loading an image, displaying, and rewrite images on the local file system.

Read, Display, and Write Image:

The below python script will load, display, and then rewrite the image back on the disk.

# Loading the OpenCV library
import cv2

# provide path of the image
image = cv2.imread("studytonight.png")

#print(image.shape)

# This below line will show the image width in pixel format. 
# We are accessing the image with the help of the shape
print("width of the image: {w} pixels".format(w=image.shape[1]))

# This below line will show the image height in pixel format
print("height of the image: {h}  pixels".format(h=image.shape[0]))

# This below line will show the image channels, if the image is colour.
print("channels of the image: {c}".format(c=image.shape[2]))

# This will show the read image on the screen with canvas name on the upper Image.
cv2.imshow("Image", image)

# The below line use basically to pause the screen.
cv2.waitKey(0)

# The below line of code used to write the image with a new name or format. Here, we don't care
# about how OpenCV converts png to jpg because it is done by OpenCV automatically. 
cv2.imwrite("newimage.jpg", image)

The shape property for the image loaded using the cv2.imread() function include the number of rows, columns, and channels; type of image data; the number of pixels; etc. The shape property of an image is accessed by img.shape where img is the reference of the image read using openCV. It returns a tuple of the number of #rows, columns, and channels.

We saved the above python script with the name of load_display_save.py and the image which we want to read is also in the same directory. If you have the image in some other folder, you need to give the complete path of the image while reading the image to the cv2.imread function.

Run the script using the following:

python load_display_save.py

The below image shows the output for the script above.

use openCV to load, display and write image

In the below output screen, we can see that there is one another image in .jpg format which is the image written to the directory by our code. So, our code works for us, and we were able to successfully load, display, and rewrite the image on disk.

use openCV to load, display and write image

Summary:

So in this tutorial, we gave you an introduction to the OpenCV library and we covered how to load the image, display the image on the screen, and again re-write file with a different name or different image formats to the file system.

In the next tutorial, we will see, how to the read image using the command line.


RELATED POSTS



Subscribe and receive amazing posts directly in your inbox.