Updated April 18, 2023
Introduction to OpenCV save image
OpenCV save image() is a method which is present in the OpenCV Public Library that enables the system to save a provided image data, which is in the form of a ndarray data into a file by making use of the imwrite() function present in the OpenCV library available for Python programming language. The OpenCV library is a public domain that has an array of methods that help in performing various operations on the image files, but for operating that it is important that the system can read the image file provided as the source/input and then the various operations can be performed on it and later after the processing is completed this file can be saved and used further by the user. While the user is working with these processed images for various applications, it is generally noted that the user needs to store the resultant intermediate images or the final resultant images while the process of transformation is occurring. For the system being able to save this processed image within the local filing system, the OpenCV save image method (cv2.imwrite()) is most commonly used.
Syntax for using OpenCV save image()
the following is the syntax that needs to be used for operating on images and saving them in the filing system:
cv2.imwrite('/path/to/destination/finalimagename.png,image)
Parameter for using OpenCV save image()
The following parameters are used within the syntax for the OpenCV save image function, which enables the command to save images within the local filing system:
Parameter | Description of the Parameter |
first argument – Path/to/image * | this parameter represents the path is selected from which to image that is to be processed by the user can be extracted for this system to read the image |
second argument – image | This parameter represents the ndarray which is responsible for containing the image file which has been entered as the source file by the user |
cv2.IMREAD_COLOR | This parameter is responsible for reading the images which have RGB colors contained in them but do not have any transparency channel present in the image. the default value for the flag parameter is set in this mode unless a second argument it’s provided for the flag parameter while using the read image function. |
cv2.IMREAD_UNCHANGED | This parameter is responsible for reading the input images that have a Gray color scheme known as the Gray images. if the primary source of the image is presumably a colored image, then in such a case, the Gray value for each of the pixels in the image is calculated by the system by averaging the optimum color channels and further the average value for each of the pixels is taken as the primary data for the array in order to read the image. |
CV_LOAD_IMAGE_ANYDEPTH | this parameter is responsible for reading the image as it is from the primary source of the image. Add it returns a 32 bits image or a 16 bits image with respect to the corresponding depth. in case the flag has not been set in the default mode, it converts the image to an 8-bits image |
Return Value | This method is responsible for returning an output image that is to be loaded from the file which has been specified. |
Formats / Extensions that are supported for OpenCV save image()
When OpenCV read image function reeds the image that has to be processed for any specific procedure to be performed by the system, it does not generally consider the extension of the image file being processed to determine the format for the image file image. rather it is seen to be deciding the extension for the image to be based upon the format for the file which has been presented in the file data respective to the image.
Window bitmaps – (eg: .bmp, .dib); JPEG files – (eg: .jpeg, .jpg, .jpe); Portable Network Graphics – (eg: .png); Portable image format– (eg: .pbm, .ppm .pgm); TIFF files – (eg: .tiff, .tif)
Following are the parameters that are being currently supported for the method:
- For files with extension .JPEG, the quality can be between 0 – 100. The default value for JPEG files is 95.
- For files with extension .PNG, the quality for the compress level can be between 0 -9. The default value for PNG files is 1.
- For files with extension PPM, PBM or PGM, there can be a binary format flag with a value of either 0 or 1. The default value for the files with these extensions is 1.
Example of OpenCV save image()
Code:
# command used to import the OpenCV library to utilize OpenCV read image function
import cv2
# command used for reading an image from the disk disk, cv2.imread function is used
# reading the image as a grey scaled image
img1 * = * cv2.imread * (* r * ' * C *:\ * Users * \ * PRIYANKA BANERJEE\ * educba.png', * * * * 1)
# saving the image into * * * the * * * filing * * * system
status1 * = * cv2.imwrite * (* r * ' * C *: * \ * Users * \ * PRIYANKA BANERJEE * \educba1.jpeg', * * 0, * img1)
print("The image file needs to be written to the file-system : ", * status1)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output for Example:
Conclusion
It is one of the most extensively used commands for processing any image file and executive any function related to image processing and detection in the Python programming language. While the user is working with these processed images for various applications, it is generally noted that the user needs to store the resultant intermediate images or the final resultant images while the process of transformation is occurring. For the system to be able to save this processed image within the local filing system, the OpenCV save image method.
Recommended Articles
We hope that this EDUCBA information on “OpenCV save image” was beneficial to you. You can view EDUCBA’s recommended articles for more information.