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

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

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

ValueError: Could not find a format to read the specified file in mode '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)

我收到这个错误:

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

最近遇到了完全相同的问题,问题是单个损坏的文件。最好是使用类似 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(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)

不同,但以防万一。我在另一个库 (skimage) 中遇到了相同的错误,解决方案是像这样添加一个额外的 'plugin' 参数 -

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

我今天遇到了这个问题,发现如果我在将文件读入 imageio 之前关闭文件,问题就会消失。

错误是:

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' 

解决方法: 将 file.close() 放在 images.append(imageio.imread(filename)) 之前,而不是放在

之后

添加选项"pilmode":

imageio.imread(filename,pilmode="RGB")

对我有用。

我也遇到了同样的错误,最后发现是图片损坏了。

我不小心将一些图片保存为PDF,所以出现了错误。删除那些不兼容格式的图像后解决。