Python Script to detect broken images

后端 未结 4 2064
深忆病人
深忆病人 2021-01-24 07:10

I wrote a python script to detect broken images and count them, The problem in my script is it detects all the images and does not detect broken images. How to fix this. I refe

4条回答
  •  情话喂你
    2021-01-24 07:56

    try the below: It worked fine for me. It identifies the bad/corrupted image and remove them as well. Or if you want you can only print the bad/corrupted file name and remove the final script to delete the file.

    for filename in listdir('/Users/ajinkyabobade/Desktop/2/'):
        if filename.endswith('.JPG'):
            try:
                img = Image.open('/Users/ajinkyabobade/Desktop/2/'+filename)  # open the image file
                img.verify()  # verify that it is, in fact an image
            except (IOError, SyntaxError) as e:
                print(filename)
                os.remove('/Users/ajinkyabobade/Desktop/2/'+filename)
    

提交回复
热议问题