Python OpenCV | cv2.imwrite() method

The cv2.imwrite() method in OpenCV is used to save an image to a specified file. It takes two arguments: the file name and the image to be saved.

Here's an example of how to use the cv2.imwrite() method:

import cv2 # Load the image img = cv2.imread('image.jpg') # Save the image to a file cv2.imwrite('saved_image.jpg', img)

The cv2.imwrite() method supports a variety of image file formats, such as JPG, PNG, and BMP. You can specify the file format by adding the appropriate file extension to the file name.

For example, to save the image as a PNG file, you can use the following code:

cv2.imwrite('saved_image.png', img)

You can also specify additional options when saving the image, such as the image quality, by using the cv2.imwrite() method's optional parameters. For example:

cv2.imwrite('saved_image.jpg', img, [cv2.IMWRITE_JPEG_QUALITY, 90])

This saves the image as a JPG file with a quality of 90%.

I hope this helps!

No comments

Powered by Blogger.