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.
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.
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With