【问题标题】:OSError: broken data stream when reading image fileOSError:读取图像文件时数据流损坏
【发布时间】:2017-07-16 16:33:26
【问题描述】:

我正在尝试使用 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))

当我运行代码时,我收到以下错误。

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

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

任何帮助将不胜感激!

【问题讨论】:

    标签: python-3.5 keras


    【解决方案1】:

    在代码的开头使用这个:

    from PIL import Image, ImageFile
    ImageFile.LOAD_TRUNCATED_IMAGES = True
    

    我找到了here。这对我有用。

    【讨论】:

      【解决方案2】:

      根据here Pillow pip install Pillow --upgrade 升级应该可以解决这个问题。

      如果您仍然遇到问题,可以使用mogrify 批量转换所有图像。 mogrify -format png *.jpg

      【讨论】:

      • 当我尝试更新枕头时,它说枕头已经是最新版本。不过我没有尝试mogrify 命令。
      猜你喜欢
      • 2015-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-15
      • 1970-01-01
      相关资源
      最近更新 更多