Python Script to detect broken images

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 refered :

How to check if a file is a valid image file? for my code

My code

import os
from os import listdir
from PIL import Image
count=0
for filename in os.listdir('/Users/ajinkyabobade/Desktop/2'):
    if filename.endswith('.JPG'):
     try:
      img=Image.open('/Users/ajinkyabobade/Desktop/2'+filename)
      img.verify()
     except(IOError,SyntaxError)as e:
         print('Bad file  :  '+filename)
         count=count+1
         print(count)

You are building a bad path with

img=Image.open('/Users/ajinkyabobade/Desktop/2'+filename)      

Try the following instead

img=Image.open('/Users/ajinkyabobade/Desktop/2/'+filename)      

or

img=Image.open(os.path.join('/Users/ajinkyabobade/Desktop/2', filename))
链接地址: http://www.djcxy.com/p/42428.html

上一篇: 在Python中使用标志时奇怪的输出

下一篇: Python脚本来检测破碎的图像