Updated April 18, 2023
Introduction to OpenCV imwrite
While we are trying to process the images in the applications of image processing, it is often very necessary to store the intermediate results to the local file system, in order to store the images in the local file system, we make use of a function called imwrite() function using which the image to be stored in the local file system is being written to the local file system and upon writing or saving the image to the local file system, the imwrite() function returns true and upon failing to save the image to the local file system, the imwrite() function returns false. In this topic, we are going to learn about OpenCV imwrite.
The syntax to define imwrite() function in OpenCV is as follows:
returnvalue = imwrite(file_path, image)
where file_path is the location of the file in which the image is to be written or saved,
image is the image to be written or saved to the file and
returnvalue is the Boolean value returned by the imwrite() function.
Working of imwrite() function in OpenCV is as follows
- While we are trying to process the images in the applications of image processing, it is often very necessary to store the intermediate results in the local file system.
- In order to write the images or save the images to the local file system, we make use of a function called the imwrite() function in OpenCV.
- The imwrite() function takes two parameters namely file_path and image.
- The parameter file_path to the imwrite() function is the location of the file in which the image is to be written or saved.
- The parameter image is the image to be written or saved to the file.
- The imwrite() function returns the Boolean value true upon successfully writing or saving the image to the local file system.
- The imwrite() function returns the Boolean value False upon failing to write the or save the image to the local file system.
Examples of OpenCV imwrite
Here are the follwoing examples mention below
Example #1
OpenCV program in python to read the image from one location using imread() function and write or save the image to another location using imwrite() function and display the return status from the imwrite() function as the output on the screen:
Code:
#import the module cv2
import cv2
#read the image from a given file location using imread() function
readimage = cv2.imread('C:/Users/admin/Desktop/logo.png', cv2.IMREAD_GRAYSCALE)
#write or save the image to a different file location using imwrite() function
result = cv2.imwrite('C:/Users/admin/Desktop/results/logo.png', readimage)
#displaying the return value from the imwrite() function
print("The image is saved to the file : ", result)
Output:
In the above program, we are importing the module cv2 and then reading the image from a given file location using the imread() function and then writing or saving the image to a different file location using the imwrite() function and then displaying the return value from the imwrite() function as the output on the screen.
Example #2
OpenCV program in python to read the image from one location using imread() function and write or save the image to another location using imwrite() function and display the return status from the imwrite() function as the output on the screen:
Code:
#import the module cv2
import cv2
#read the image from a given file location using imread() function
readimage = cv2.imread('C:/Users/admin/Desktop/tree.jpg', cv2.IMREAD_GRAYSCALE)
#write or save the image to a different file location using imwrite() function
result = cv2.imwrite('C:/Users/admin/Desktop/results/tree.jpg', readimage)
#displaying the return value from the imwrite() function
print("The image is saved to the file : ", result)
Output:
In the above program, we are importing the module cv2 and then reading the image from a given file location using the imread() function and then writing or saving the image to a different file location using the imwrite() function and then displaying the return value from the imwrite() function as the output on the screen.
Example #3
OpenCV program in python to read the image from one location using imread() function and write or save the image to another location using imwrite() function and display the return status from the imwrite() function as the output on the screen:
Code:
#import the module cv2
import cv2
#read the image from a given file location using imread() function
readimage = cv2.imread('C:/Users/admin/Desktop/plane.jpg', cv2.IMREAD_GRAYSCALE)
#write or save the image to a different file location using imwrite() function
result = cv2.imwrite('C:/Users/admin/Desktop/results/plane.jpg', readimage)
#displaying the return value from the imwrite() function
print("The image is saved to the file : ", result)
Output:
In the above program, we are importing the module cv2 and then reading the image from a given file location using the imread() function and then writing or saving the image to a different file location using the imwrite() function and then displaying the return value from the imwrite() function as the output on the screen. The output is shown in the snapshot above.
Example #4
OpenCV program in python to read the image from one location using imread() function and write or save the image to another location using imwrite() function and display the return status from the imwrite() function as the output on the screen:
Code:
#import the module cv2
import cv2
#read the image from a given file location using imread() function
readimage = cv2.imread('C:/Users/admin/Desktop/car.jpg', cv2.IMREAD_GRAYSCALE)
#write or save the image to a different file location using imwrite() function
result = cv2.imwrite('C:/Users/admin/Desktop/results/car.jpg', readimage)
#displaying the return value from the imwrite() function
print("The image is saved to the file : ", result)
Output:
In the above program, we are importing the module cv2 and then reading the image from a given file location using the imread() function and then writing or saving the image to a different file location using the imwrite() function and then displaying the return value from the imwrite() function as the output on the screen.
Recommended Articles
We hope that this EDUCBA information on “OpenCV imwrite” was beneficial to you. You can view EDUCBA’s recommended articles for more information.