How to write an image to a file using OpenCV?


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

Algorithm

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

Example Code

import cv2
import os
image = cv2.imread('testimage.jpg')
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

373 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements