0

I am writing a simple program that reads and does some operations with a series of images. One of the input images has some faults (I think it's the result of disconnecting thumb drive during data transfer).

So the only statement involving the input image that does not cause the program to freeze is cv2.imread(). The moment my program reaches any statement that involves the input, it freezes. I tried putting these statements in try-catch blocks (so that error is thrown and this image is skipped) but nothing changes. Is there anything I can do to make my program see the error in the input then ignore it and go to the next image instead of freezing?

annena
  • 921
  • 1
  • 11
  • 22

1 Answers1

0

As per OpenCV doc:

The function imread loads an image from the specified file and returns it. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix ( Mat::data==NULL ).

Therefore, just check for None value, for example:

for fname in fnames:

    img = cv2.imread(fname)
    if img is None:
        continue

    # Do your processing here
Velimir Mlaker
  • 10,664
  • 4
  • 46
  • 58