How to use the imwrite () method in OpenCV

Image processing and computer vision are performed using OpenCV libraries. It has a modular structure which means it has several shared and static libraries used for the same.

Today in this article we are going to learn what and How cv2.imwrite() is used in OpenCV. 

imwrite() is used to save images after completing certain operations during image processing tasks for computer vision applications or programs. We can also use it to change the extension of an image file. This method takes two parameters to execute its operation.


Syntax: cv2.imwrite(filename, image) 

*// instead of filename we can provide a File path under inverted commas (“ ”).

Parameters:

filename: A string representing the file name. The filename must include image formats like .jpg, .png, etc.

image: It is the image that is to be saved.

Return Value: It returns true if the image is saved successfully.

Step 1:- Setup the environment 

To set up the environment you need to have 

  1. Python installed in your system

    1. Install OpenCV library

    2. Install Numpy library

    3. Install Jupyter notebook

I am assuming learners already have installed these dependencies in their systems.

For this and upcoming topics we are going to use Jupyter notebook.

Step 2:- Open your Jupyter notebook  by simply using the command in your terminal

Command:- Jupyter notebook

It will prompt a new window with your Jupyter environment shown below

Step 3:- Import the library by using the command shown below


import cv2
import numpy as np
import os

To write any image we have two states

  1. File does not exist in your system

  2. File (video or image with different extension) exists in your system

File does not exist in your system

If the file does not exist in your system then we use a NumPy function 

Syntax = random.randint(low, high=None, size=None, dtype=int)

This function will create a random NumPy array which creates the file to become an image for storage as shown below

Step 4:- create a random NumPy array matrix to create the image and store it


ran_array = np.random.randint(255, size = (400,800,4) )

cv2.imwrite("random_img.png", ran_array)

This method will store your image in your current working directory as random_img.png if you don't define a path.

Now to check whether our image is being created and saved or not we use two more methods for verification.

  1. cv2.imread() // it is used to read the saved file 

  2. cv2.imshow() // it is used to render the save and display that image.

Step 5:- Verify the image stored


img = cv2.imread("random_img.png")
cv2.imshow("random image", img)

As soon as your code executes it will prompt a new window with an output image that has been created using a random NumPy array and stored by using imwrite() in your current working directory.

File (video or image with different extension) exist in your system

As you can see in my current directory I have only one image with the name Board_Infinity.Png. We will work with this image for our scenario.

Step 1:- To work with this image we’ll first read that image by providing the path to a variable img_path and then read the image using cv2.imread().

Step 2:-  now we’ll use cv2.imwrite() to show our output image with the windows name “convert_image”

import cv2
import numpy as np
import os

img_path = "Board_Infinity.png"
img = cv2.imread("random_img.png")

cv2.imread("convert_image", img)
cv2.imwrite("Board_Infinity.jpg", img)

cv2.waitKey(0)
cv2.destroyAllWindows()


After using the above program for our operation we’ll notice an output window with a name.

To verify that our operation was successful or not we can check our current directory whether our image resides with a different extension or not.  Below image shows that our operation was successful.

This method is also used to store the image from a video as per the frame rate used in that video and store it in your system. We can use image extension conversion applications.

Code:-

import cv2
import numpy as np
import os


ran_array = np.random.randint(255, size = (400,800,4) )
cv2.imwrite("random_img.png", ran_array)

img = cv2.imread("random_img.png")
cv2.imshow("random image", img)

cv2.waitKey(0)
cv2.destroyAllWindows()


import cv2
import numpy as np
import os

img_path = "Board_Infinity.png"
img = cv2.imread("random_img.png")

cv2.imread("convert_image", img)
cv2.imwrite("Board_Infinity.jpg", img)

cv2.waitKey(0)
cv2.destroyAllWindows()


Post a Comment

0 Comments