已解决,今天碰到这个问题,感谢scool_winter的博文
自己简单记录一下:
报错原因:图像文件是损坏的,所以无法读取。
解决办法:
1、找到损坏的图片,并删除
打印当前的图片名
print(imgname)
如下图,这个被损坏的图片,把它从文件夹中删除,程序就能正常运行。
2、在程序开头加上如下代码
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
探究原因
查看错误发生的代码处,代码文件是 ImageFile.py ,位于python的site-package文件夹内,下面是报错部分代码
try:
# FIXME: This is a hack to handle TIFF's JpegTables tag.
prefix = self.tile_prefix
except AttributeError:
prefix = b""
for decoder_name, extents, offset, args in self.tile:
decoder = Image._getdecoder(self.mode, decoder_name,
args, self.decoderconfig)
seek(offset)
decoder.setimage(self.im, extents)
if decoder.pulls_fd:
decoder.setfd(self.fp)
status, err_code = decoder.decode(b"")
else:
b = prefix
while True:
try:
s = read(self.decodermaxblock)
except (IndexError, struct.error): # truncated png/gif
if LOAD_TRUNCATED_IMAGES:
break
else:
raise IOError("image file is truncated")
if not s: # truncated jpeg
self.tile = []
# JpegDecode needs to clean things up here either way
# If we don't destroy the decompressor,
# we have a memory leak.
decoder.cleanup()
if LOAD_TRUNCATED_IMAGES:
break
else:
raise IOError("image file is truncated "
"(%d bytes not processed)" % len(b))