OSError:读取图像文件时损坏的数据流 [英] OSError: broken data stream when reading image file

查看:94
本文介绍了OSError:读取图像文件时损坏的数据流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to read an image file using Image package of Keras.

这是我的代码.

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))

当我运行代码时,出现以下错误.

When I run the code, I get the following error.

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 格式.在网上搜索解决方案后,我尝试了几种方法,但无法解决问题.

Please note, if I convert test_image.jpg to test_image.png, then the given code works perfectly. But I have several thousands of pictures and I can't convert all of them to png format. I tried several things after searching for solution in web but couldn't get rid of the problem.

任何帮助将不胜感激!

推荐答案

在代码的开头使用:

from PIL import Image, ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True

我在这里找到了它.这对我有用.

I found it here. And this is working for me.

这篇关于OSError:读取图像文件时损坏的数据流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆