AppDividend
Latest Code Tutorials

Python cv2 imwrite() | Python cv2 Save Image Example

0

To save or write an image in Python, use cv2.imwrite() function provided by Python OpenCV library. To get started, we have to import cv2 module, which will make available the functionalities required to read the original image and save the image as a different name or write a new image.

Python cv2 imwrite()

The cv2.imwrite() method in Python is used to save the image to any storage device.

This will save an image according to the particular format in the current working directory.

When we are working with images in Image Processing applications, it is quite often that you have to save the intermediate results of image transformations or save the resulting final image.

When working with OpenCV Python, images are stored in the numpy ndarray.

To save an image to the local file system, use cv2.imwrite() function of opencv python library.

Let’s see the syntax of the imwrite() method.

Syntax

cv2.imwrite(filename, image)

Parameters

The imwrite() function takes the following two parameters.

  1. filename: It is a string representing the file name. The filename must include the image format like .jpg, .png, etc.
  2. Image: It is an image that is to be saved.

Return Value

The imwrite() function returns True if the image is saved successfully.

Example

Let’s write a program that saves the image from an existing image in the current project directory.

# importing cv2 module
import cv2

# Image path
image_path = './forest.jpg'

# Using cv2.imread() method to read the image
img = cv2.imread(image_path)

# New image Filename
filename = 'saved_new.jpg'

# Using cv2.imwrite() method saving the image
cv2.imwrite(filename, img)

print('The image is saved Successfully')

Output

The image is saved Successfully

 

Python cv2 imwrite()

In this example, we have imported the cv2 module and then defined an input image path.

Then we used cv2.imread() function to read image and use the imwrite() method to save the file.

Reading the return value of the imwrite() method is fundamental as sometimes there could be multiple reasons that fail a disk write operation and resulting in the image not written to disk.

Conclusion

Python cv2.imwrite() method saves an image in the local filesystem. The imwrite() function saves an image to the specified file. The image format is chosen based on a filename extension (see imread() for a list of extensions).

Only 8-bit (or 16-bit unsigned (CV_16U) in case of PNG, JPEG 2000, and TIFF) single-channel or 3-channel (with ‘BGR’ channel order) images can be saved using the imwrite() method.

If the format, depth or channel order is different then use the Mat::convertTo() , and cvtColor() to convert it before saving. Or, use the universal FileStorage I/O functions to store the image to XML or YAML format.

It is possible to save PNG images with the alpha channel using the imwrite() function. To do this, create an 8-bit (or 16-bit) 4-channel image BGRA, where an alpha channel goes last.

Fully transparent pixels should have alpha set to 0, fully opaque pixels should have alpha set to 255/65535.

That is it for the Python cv2.imwrite() method.

See Also

Convert Image to Base64 String

Convert PIL Image to Grayscale

Convert PIL Image to Numpy Array

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.