python - raise ValueError( --> 182 "在 %s 模式下找不到读取指定文件的格式" % modename
问题描述
这两个代码都会引发错误。我不确定确切的原因是什么?我正在使用 Jupyter 笔记本运行。
from __future__ import print_function, division
import os
import torch
import torch.nn as nn
import torchvision
from torch.autograd import Variable
import pandas as pd
from skimage import io, transform
import numpy as np
import matplotlib.pyplot as plt
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils
def show_landmarks(image, landmarks):
"""Show image with landmarks"""
plt.imshow(image)
plt.scatter(landmarks[:, 0], landmarks[:, 1], s=80, marker='.', c='b')
plt.pause(0.001) # pause a bit so that plots are updated
plt.figure()
img_name = "faces/person-7.jpg"
print(img_name)
img = io.imread(img_name)
print('here')
show_landmarks(img, landmarks)
plt.show()
###def show_landmarks(image, landmarks):
### """Show image with landmarks"""
### plt.imshow(image)
### plt.scatter(landmarks[:, 0], landmarks[:, 1], s=10, marker='.', c='r')
### plt.pause(0.001) # pause a bit so that plots are updated
###plt.figure()
###show_landmarks(io.imread(os.path.join('data/faces/', img_name)),
### landmarks)
###plt.show()
错误是:
faces/person-7.jpg
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-14-972bef8edc6f> in <module>
9 print(img_name)
10
---> 11 img = io.imread(img_name)
12 print('here')
13 show_landmarks(img, landmarks)
~/anaconda3/lib/python3.7/site-packages/skimage/io/_io.py in imread(fname, as_gray, plugin, **plugin_args)
46
47 with file_or_url_context(fname) as fname:
---> 48 img = call_plugin('imread', fname, plugin=plugin, **plugin_args)
49
50 if not hasattr(img, 'ndim'):
~/anaconda3/lib/python3.7/site-packages/skimage/io/manage_plugins.py in call_plugin(kind, *args, **kwargs)
207 (plugin, kind))
208
--> 209 return func(*args, **kwargs)
210
211
~/anaconda3/lib/python3.7/site-packages/skimage/io/_plugins/imageio_plugin.py in imread(*args, **kwargs)
8 @wraps(imageio_imread)
9 def imread(*args, **kwargs):
---> 10 return np.asarray(imageio_imread(*args, **kwargs))
~/anaconda3/lib/python3.7/site-packages/imageio/core/functions.py in imread(uri, format, **kwargs)
263
264 # Get reader and read first
--> 265 reader = read(uri, format, "i", **kwargs)
266 with reader:
267 return reader.get_data(0)
~/anaconda3/lib/python3.7/site-packages/imageio/core/functions.py in get_reader(uri, format, mode, **kwargs)
180 modename = MODENAMES.get(mode, mode)
181 raise ValueError(
--> 182 "Could not find a format to read the specified file in %s mode" % modename
183 )
184
ValueError: Could not find a format to read the specified file in single-image mode
<Figure size 432x288 with 0 Axes>
解决方案
该错误似乎与第 11 行有关 - img = io.imread(img_name)。可能会引发此错误,因为您没有将可选的pluginstr与imread一起使用,或者因为imread找不到合适的插件来读取图像文件。
skimage.io.imread 带参数:
skimage.io.imread(fname, as_gray=False, plugin=None, **plugin_args)
pluginstr, optional
Name of plugin to use. By default, the different plugins are tried (starting with imageio) until a suitable candidate is found. If not given and fname is a tiff file, the tifffile plugin will be used.
您可以通过尝试各种插件来解决错误,如下例所示:
io.imread(f.name, plugin="tifffile")
这是可用插件的列表
推荐阅读
- spring-cloud - Spring Cloud Gateway 在通过谷歌网关后转发 Cookie
- telegram - 如何使用python电报机器人转发所有媒体组
- python-3.x - 字符串格式的 Pandas 列表到 dict 列表
- python-3.x - wxPython 应用程序不退出系统托盘图标左键单击
- terraform - 将多个库附加到集群地形化 Databricks
- angular - 有没有更好的方法来验证复选框选择 Angular?
- google-bigquery - 获取 BQ 查询历史数据
- android - 取消所有安卓用户的活跃订阅
- python - 将小时和天添加到 python 日期时间
- php - 我可以使用 Laravel 在产品模型中使用餐厅和类别表的多对多关系吗?以及如何添加更新 Categories_Restaurants 表?