cv2.imread: checking if image is being read

If you’re sure that the value of img is None in your case, you can simply use if not img is None, or, equivalently, if img is not None. You don’t need to check the type explicitly.

Note that None and False are not the same value. However, bool(None)==False, which is why if None fails.

The documentation for imread, both for OpenCV 2 and 3, states, however, that a empty matrix should be returned on error. You can check for that using if img.size ==0

Leave a Comment