ValueError:找不到在模式“ i”下读取指定文件的格式

Cal*_*tta 3 python-3.x python-imageio

我正在尝试将png文件读入在docker中运行的python-flask应用程序中,并收到一条错误消息,内容为

ValueError:找不到在模式“ i”下读取指定文件的格式

我已经使用HTML文件上传了文件,现在我正尝试阅读该文件以进行进一步处理。我看到scipy.misc.imread已被弃用,我正尝试将其替换为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)
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

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
Run Code Online (Sandbox Code Playgroud)

小智 7

最近遇到了完全相同的问题,问题是单个损坏的文件。最好是使用 PIL 之类的东西来检查坏文件。

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(dir_path+"\\"+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(dir_path+"\\"+filename) (Maybe)
Run Code Online (Sandbox Code Playgroud)


小智 5

不同,但以防万一。我在不同的库(skimage)中有一个相同的错误,解决方案是添加一个额外的“ plugin”参数,如下所示:

image = io.imread(filename,plugin='matplotlib')
Run Code Online (Sandbox Code Playgroud)