Skip to content
Related Articles

Related Articles

Python OpenCV – haveImageWriter() function

View Discussion
Improve Article
Save Article
  • Last Updated : 01 May, 2022

In this article, we are going to learn about the haveImageWriter() function of the OpenCV library.

haveImageWriter() function

Sometimes we need to detect if the specified image file is being correctly written or not before continuing further, In such a case we can use OpenCV which helps us to process images and videos to identify objects. When openCV is integrated with methods like the haveImageWriter() function. It helps us to check whether specified images can be encoded or written successfully or not. below is the syntax of the haveImageWriter function.

Syntax: cv.haveImageWriter(filename)

Parameters:

  • filename: Filename of the image.

Return: True, if specified image written successfully otherwise it returns False.

Example 1: 

We will use the following image extension in our code and check whether it can be read correctly or not. Any image file with extension jpg, png, and jpeg will return True.

Python3




# Import the library OpenCV
import cv2
  
# Use haveImageWriter() function to check
# provided image file can be encoded or not
return_val = cv2.haveImageWriter("gfg.png")
  
# Print the returned value
print(return_val)


Output:

True

Example 2: 

Any image file with an extension with a GIF will return False. 

Python3




# Import the library OpenCV
import cv2
  
# Use haveImageWriter() function to check
# provided image file can be encoded or not
return_val = cv2.haveImageWriter("apple.gif")
  
# Print the returned value
print(return_val)


Output:

False

Example 3: 

Any image file with extensions other than jpg, png, and jpeg will return False.

Python3




# Import the library OpenCV
import cv2
  
# Use haveImageWriter() function to check
# provided image file can be encoded or not
return_val = cv2.haveImageWriter("apple.mp4")
  
# Print the returned value
print(return_val)


Output:

False

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!