cv2.imwrite Usage
What’s the right time to use this method?
-
Processing batch images or real-time computer vision is a complex process. After this processing is done, only then the role of this method comes. Its use is to post-process the image based on the extension and in the end, save the image in the local file system.
-
Another use case of this function is debugging. While designing some image processing algorithms, bugs might occur. To see what's going on, the developer might need to see the processed image.
- But even during this debugging use case, its purpose remains to save input images into the local storage.
cv2.imwrite Image
-
This argument expects a NumPy array that contains information about the image.
- This array is contained in the variable in which you stored the image during imread
img = cv2.imread('./data.img2.png')
- NumPy array is nothing but a regular array with some additional functions attached to it for better and faster image processing.
cv2.imwrite Options
You can provide several optional parameters as the third argument in the function. These options can be very useful in various cases like saving storage space, etc.
-
Option names consist of 3 parts:
- IMWRITE - function name.
-
JPEG/PNG/WEBP/TIFF - image extension
-
COMPRESSION/QUALITY, etc - property
- All 3 are in upper case and are connected by underscore ‘_’
- Eg, IMWRITE_JPEG_QUALITY
-
These options can be accessed using cv2 - cv2.<option_name>
-
Passing an option:
options = [option1, value, option2, value, ......]
cv2.imwrite(path, image, options)
-
Commonly used options (classified by extension):
-
JPEG/WEBP
- Image quality in %
-
Eg. [IMWRITE_JPEG_QUALITY, 50]
-
PNG
- Image compression level
-
Eg. [COMPRESSION, 4]
-
TIFF
- Image compression type
-
Values:
- IMWRITE_TIFF_UNCOMPRESSED (no compression)
- IMWRITE_TIFF_LZW
- IMWRITE_TIFF_PACKBITS
- [IMWRITE_TIFF_COMPRESSION, IMWRITE_TIFF_PACKBITS]
Under The Hood
The following things are done under the hood when you call the imwrite function:
-
The destination path is processed and the file extension is extracted from it.
-
If the input image and the output image had different formats, eg. jpg and png, it performs the necessary tasks to convert one into another.
-
If options are provided it processes the image according to them.
-
The image object is encoded into the required format using the correct codec library.
-
Now that everything is ready, it saves the image to your local file system.
- It then unlocks any locked resources and closes connections.
Frequently Asked Questions
Why should I use the imwrite function instead of the PIL save function?
It provides many functions that are not provided by the ‘save’ function. It makes us consistent with cv2. It is much faster and optimised for the task of image post-processing and saving.
What exceptions does the imwrite method throw?
This method does not throw any exceptions. If it ends in an error, it simply returns False. Therefore you must be very careful while working with this method.
What is the OpenCV Imwrite default quality?
The OpenCV Imwrite default JPEG quality ranges from 0 to 100. The higher is the number, the better is the quality. Generally, 95 is the default value.
What is the difference between Imwrite and Imshow in cv2?
imwrite in OpenCV saves an image to disk, specifying its file format and quality, while imshow displays an image in a window for visualization. imwrite is for saving images, while imshow is for viewing them. Both are essential for image processing tasks, like saving processed images or displaying results.
Conclusion
In this article, we read about the cv2.imwrite method which is the go-to function if you’re using OpenCV in complete detail.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.