2017-02-25 87 views
1

當我試圖讀取使用Keras的圖片包的圖像文件中斷數據流。OSERROR:讀取圖片文件

這是我的代碼。

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.jpgtest_image.png,然後給定的代碼完美的作品。但我有幾千張照片,我無法將它們全部轉換爲png格式。在網絡中搜索解決方案後,我嘗試了幾件事,但無法擺脫問題。

任何幫助,將不勝感激!

回答

1

使用此:

from PIL import Image, ImageFile 
ImageFile.LOAD_TRUNCATED_IMAGES = True 

我發現它here。這對我有用。

0

根據herePillow升級由pip install Pillow --upgrade應該解決這個問題。

如果你仍然面臨的問題,您可以使用mogrify批量轉換的所有圖片。在你的代碼的開頭mogrify -format png *.jpg

+0

,當我嘗試更新的枕頭,它說,枕頭已經是其最新版本。儘管我沒有嘗試'mogrify'命令。 –