OSError:读取图像文件时数据流中断

Was*_*mad 2 python-3.5 keras

我正在尝试使用Keras的Image包读取图像文件。

这是我的代码。

from keras.preprocessing import image
img_path = 'test/test_image.jpg'  # This is an image I took in my kitchen.
img = image.load_img(img_path, target_size=(224, 224))
Run Code Online (Sandbox Code Playgroud)

运行代码时,出现以下错误。

anaconda3/lib/python3.5/site-packages/PIL/ImageFile.py in load(self)
    238         if not self.map and not LOAD_TRUNCATED_IMAGES and err_code < 0:
    239             # still raised if decoder fails to return anything
--> 240             raise_ioerror(err_code)
    241 
    242         # post processing

anaconda3/lib/python3.5/site-packages/PIL/ImageFile.py in raise_ioerror(error)
     57     if not message:
     58         message = "decoder error %d" % error
---> 59     raise IOError(message + " when reading image file")
     60 
     61 

OSError: broken data stream when reading image file
Run Code Online (Sandbox Code Playgroud)

请注意,如果我转换test_image.jpgtest_image.png,则给定的代码可以完美运行。但是我有数千张图片,我无法将它们全部转换为png格式。在网络中搜索解决方案后,我尝试了几件事,但无法摆脱问题。

任何帮助,将不胜感激!

Pre*_*rko 6

在代码开头使用此代码:

from PIL import Image, ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
Run Code Online (Sandbox Code Playgroud)

我在这里找到的。这对我有用。