ValueError: Could not find a format to read the specified file in mode 'i'

这一生的挚爱 提交于 2019-12-07 12:21:00

问题


I am trying to read a png file into a python-flask application running in docker and am getting an error that says

ValueError: Could not find a format to read the specified file in mode 'i'

i have uploaded a file using an HTML file and now i am trying to read it for further processing. i see that scipy.misc.imread is deprecated and i am trying to replace this with imageio.imread

if request.method=='POST':
    file = request.files['image']
    if not file: 
        return render_template('index.html', label="No file")
    #img = misc.imread(file)
    img = imageio.imread(file)

i get this error :

File "./appimclass.py", line 34, in make_prediction

img = imageio.imread(file)

File "/usr/local/lib/python3.6/site-packages/imageio/core/functions.py", line 221, in imread

reader = read(uri, format, "i", **kwargs)

File "/usr/local/lib/python3.6/site-packages/imageio/core/functions.py", line 139, in get_reader

"Could not find a format to read the specified file " "in mode %r" % mode

回答1:


Had the exact same problem recently, and the issue was a single corrupt file. Best is to use something like PIL to check for bad files.

import os
from os import listdir
from PIL import Image

dir_path = "/path/"


for filename in listdir(dir_path):
    if filename.endswith('.jpg'):
        try:
            img = Image.open(base_dir+"\\"+filename) # open the image file
            img.verify() # verify that it is, in fact an image
        except (IOError, SyntaxError) as e:
            print('Bad file:', filename)
            #os.remove(base_dir+"\\"+filename) (Maybe)



回答2:


Different, but in case helpful. I had an identical error in a different library (skimage), and the solution was to add an extra 'plugin' parameter like so -

image = io.imread(filename,plugin='matplotlib')



回答3:


I had this problem today, and found that if I closed the file before reading it into imageio the problem went away.

Error was:

File "/home/vinny/pvenvs/chess/lib/python3.6/site-packages/imageio/core/functions.py", line 139, in get_reader                                                  "Could not find a format to read the specified file " "in mode %r" % mode  ValueError: Could not find a format to read the specified file in mode 'i' 

Solution: Put file.close() before images.append(imageio.imread(filename)), not after.



来源:https://stackoverflow.com/questions/54379176/valueerror-could-not-find-a-format-to-read-the-specified-file-in-mode-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!