Understanding cv2.imwrite() in OpenCV Python

In order to solve computer vision problems, we use one of the biggest open-source library known as OpenCV. It contains a collection of computer vision and machine learning software that accelerates the use of machine perception in commercial products.

cv2.imwrite(path, image)

cv2.imwrite() is one of the function of openCV library that is used to save the resulting or the transformed image into a specific file or folder. It takes two arguments :

  • path : It is the destination of a specific file or a folder where image is required to be saved.
  • image: The second argument is the image that is to be saved.

Returns true if image is saved successfully.

Example:

import cv2
img = cv2.imread('D:\Desktop Projects\hacker.png', cv2.IMREAD_GRAYSCALE)
status = cv2.imwrite('D:\Desktop Projects\grey_hacker.png',img)
print("Image status : ",status)

Output:

Image status : True

Explanation

In the above example we first read the image that is to be saved using imread() function in grey scale. After that we used the imwrite() function to save the transformed image with different name to a specified location. Finally we returned the status of the image saved.

The above code when run returns True which means that the file is successfully save in the format and the path we want. Now check the status manually by migrating to the particular directory or using command prompt to find the new image saved.

(Note : While running above code in your system kindly use your own file path and not the one specified here.)

Leave a Reply

Your email address will not be published. Required fields are marked *