python check if image is corrupted

Solutions on MaxInterview for python check if image is corrupted by the best coders in the world

showing results for - "python check if image is corrupted"
Helena
08 Jul 2016
1from os import listdir
2from PIL import Image
3    
4for filename in listdir('./'):
5  if filename.endswith('.png'):
6    try:
7      img = Image.open('./'+filename) # open the image file
8      img.verify() # verify that it is, in fact an image
9    except (IOError, SyntaxError) as e:
10      print('Bad file:', filename) # print out the names of corrupt files