Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if python cv2.imwrite worked properly

I would like to check if cv2.imwrite executed properly and my file was saved. I tried to use exception handling with try-except, but if, for example, there is no folder named foo/ in any case it would be printed: "Image written"

try:
    cv2.imwrite("foo/img.jpg", myImage)
    print("Image written")
except:
    print("Problem")

P.S. Checking after saving is not an option, because the file could be overwritten.

like image 615
Serob_b Avatar asked Aug 12 '18 12:08

Serob_b


1 Answers

Python cv2.imwrite() has a return value, True if the write succeeded, False if it failed.

So do something like

writeStatus = cv2.imwrite("foo/img.jpg", myImage)
if writeStatus is True:
    print("image written")
else:
    print("problem") # or raise exception, handle problem, etc.
like image 88
paisanco Avatar answered Sep 22 '22 17:09

paisanco