Opencv Python Save Image - Cv2.Imwrite
Opencv Python Save Image - Cv2.Imwrite
imwrite()
In this tutorial, we will learn how to save image data from ndarray to a file, in OpenCV Python using imwrite()
function, with an example.
While working with images in Image Processing applications, it is quite often that you need to store
intermediate results of image transformations or save the final resulting image. When working with OpenCV
Python, images are stored in numpy ndarray. To save an image to the local file system, use cv2.imwrite()
function of opencv python library.
Syntax of cv2.imwrite()
Following is the syntax of cv2.imwrite() function
cv2.imwrite('/path/to/d
estination/image.png',i
mage)
cv2.imwrite('/path/to/destination/image.png',image)
where
First Argument is Path to the destination on file system, where image is ought to be saved.
Second Argument is ndarray containing image
Returns True is returned if the image is written to file system, else False.
import cv2
print("Image written to
print("Image: written
file-system ",status)to file-system : ",status)
Run the above python script.
Output
cv2.imwrite() returned true which means the file has been successfully written to the path specified. Reading
the return value of imwrite() is very important as sometimes there could be multiple reasons that fail the disk
write operation and resulting in the image not written to disk.
On File System
arjun@localhost:/home
/img$ ls python*
python_grey.png
python.png
arjun@localhost:/home/img$ ls python*
python_grey.png python.png
Concluding this OpenCV Python Tutorial, we have successfully saved the image with specified name using
cv2.imwrite() function.
Image Processing