Programmatically Detect Corrupted Image
Published in
1 min readAug 20, 2017
When we download a huge image dataset from the internet, occasionally we may encounter image corruption due to incomplete download. For example:
In [1]: import cv2
In [2]: img = cv2.imread('corrupted.jpg')
Premature end of JPEG file
Here we demonstrate how to programmatically capture this error before sending these corrupted images into neural network.
from skimage import io
def verify_image(img_file):
try:
img = io.imread(img_file)
except:
return False
return True
Unlike cv2.imread, which only print error message, and fill corrupted image with grey color.
skimage.io will throw exception and allow us to detect and download again.
OSError: image file is truncated (3 bytes not processed)