How to write an image to a file using OpenCV?

OpenCVServer Side ProgrammingProgramming

In this program, we will write an image or save an image to a file using OpenCV.

Algorithm

Step 1: Import cv2
Step 3: Save the image using opencv.imwrite(filename, image)

Example Code

import cv2
import os
directory = r'C:\Users\prasa\Desktop'
os.chdir(directory)
cv2.imwrite('CAMERAMAN.jpg', image)

Output

This program will save the image in the directory as same as the original image directory

Explanation

Ensure that you have set the proper directory in order for the program to execute without errors.

Updated on 17-Mar-2021 07:59:02