OpenCV cv2.imwrite() Method

Learn via video courses
Topics Covered

Overview

A function that is used for displaying a picture in a window is the cv2.imwrite() method. In this blog, we will learn about the write () method in the opencv-python module, the write () method, which is used to write an image which is an array format that is to be saved as a file in the target specified location.

Syntax of OpenCV cv2.imwrite() Method

Here is the syntax of the imwrite() method in OpenCV Python.

Parameters of OpenCV cv2.imwrite() Method

Let's see the detailed explanation of the parameters available in the write () method in OpenCV.

  • filename_path: The file name is represented by text with .jpg,.png, and other picture formats that must be included in the title. Where path is the complete path of the output file to which you would like to write the image numpy array.
  • Example : "D:/new_img.png"
  • image: The image name or the path of the image that is to be stored in the new location with the new name is given here is to be entered here.
  • The Params in the cv2.imwrite() method is the optional parameters, like the resolution of the image, and size can be changed here. We can define it if needed, or else we can leave it and pass the other two major parameters.
  • The filename_path and the image parameters are the necessary parameters. If any one of these parameters is missed, then our cv2.imwrite() method throws an error.

Return Value of OpenCV cv2.imwrite() Method

Now we are going to see the return values of the cv2.imwrite() method in OpenCV.

  • Return Value: cv2.imwrite() returns a boolean value.
  • The cv2.imwrite() in OpenCV returns True if the image is successfully written to the specified local path.
  • The cv2.imwrite() in OpenCV returns False if the image is not written successfully to the specified local path.

Exceptions in cv2.imwrite() Method:

  • In this cv2.imwrite() function, there is a constraint as it has two parameters as filename_path and image.
  • If any one of these parameters is missed then the cv2.imwrite() function will throw an cv2.error

How Does the OpenCV cv2.imwrite() Method Work?

It's important to understand the working of cv2.imwrite() in OpenCV; only then can we use it wisely whenever needed. Let's see in detail how the cv2.imwrite() function works,

  • A picture can be written to a file in various file formats, including JPEG, PNG, BMP, TIFF, and others, using OpenCV's cv2.imwrite() function.

  • When you use the cv2.imwrite() method with a picture and a filename, the method first verifies the validity of the file location and the feasibility of creating the file. After successfully creating a file, the technique examines the file name to identify the picture format. To encode the image data and send it to the file, the technique chooses the best image encoding algorithm based on the format.

  • The cv2.imwrite() method also has many other parameters that can be used to define the image's encoding characteristics. The params parameter, for example, can be used to define the compression amount or other image encoding characteristics.

  • It should be noted that the cv2.imwrite() technique does not alter the initial image data. Instead, it makes a new file and saves the picture info. If you want to change the image data, you should first duplicate it with the cv2.copy()` method and then adjust the copy.

Examples

Example 1: Save Matrix as Image – cv2 imwrite()

Output:

Explanation:

  1. In the above example, we are importing the OpenCV module as cv2. Then using a variable named img is to store the initial image that is to be saved in another location with the help of the read () method in cv2, which is an image reading function with the variable name "img". Then pass that "img" variable to the cv2.imwrite() method with the new location as "D:/new_img.png" and with the new file name as "new_img.png."
  2. If the image is successfully stored, then the cv2.imwrite() function returns "True" and it is stored in the isDone variable and prints the Successful message.

Example 2: Save picture using cv2 imwrite() – using Random Values

Output:

Explanation:

  1. In Example 2, we are generating an image using the random numbers as we are generating an array of random numbers with the help of the np.random.randint() method in numpy.
  2. Then pass that random numbers array to the cv2.imwrite() method with the new location as "D:/img_2.png".
  3. Then the cv2.imwrite() method returns True if the image is successfully saved, is saved in the isDone variable, and the success message is printed.

Output: cv2 write

Conclusion

  • An picture can be written to a file using OpenCV's imwrite(), a straightforward yet effective technique.
  • The imwrite() method provides a great deal of freedom and control over the encoding characteristics of the picture, thanks to its different parameters.
  • The imwrite() function in OpenCV is a crucial tool to have, whether you are storing pictures for machine learning or computer vision tasks or just for personal use.

See Also

OpenCV python module reference

OpenCV python imwrite() documentation