skimage.io.imread() 中的值错误

ValueError in skimage.io.imread()

我在进行图像处理时收到(ValueError:无法找到以 'i' 模式读取指定文件的格式):

import matplotlib.pyplot as plt
from skimage.color import rgb2gray
from skimage.transform import resize
from skimage.io import imread, imshow
import pandas as pd
import numpy as np
import os
import warnings
warnings.simplefilter('ignore')


%matplotlib inline

p1 = os.listdir("C:/Users/IMA/Documents/ML/image/p1")
p2 = os.listdir("C:/Users/IMA/Documents/ML/image/p2")

limit = 20
p1_image = [None]*limit
j = 0
for i in p1:
    if(j < limit):
        p1_image[j] = imread("C:/Users/IMA/Documents/ML/image/p1/"+i)
        j += 1
    else:
        break
ValueError                                Traceback (most recent call last)
<ipython-input-7-1854baa31099> in <module>
      4 for i in p1:
      5     if(j<limit):
----> 6         p1_image[j]=imread("C:/Users/IMA/Documents/ML/image/p1/"+i)
      7         j+=1
      8     else:

~\anaconda3\lib\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\site-packages\skimage\io\manage_plugins.py in call_plugin(kind, *args, **kwargs)
    208                                (plugin, kind))
    209 
--> 210     return func(*args, **kwargs)
    211 
    212 

~\anaconda3\lib\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\site-packages\imageio\core\functions.py in imread(uri, format, **kwargs)
    262 
    263     # Get reader and read first
--> 264     reader = read(uri, format, "i", **kwargs)
    265     with reader:
    266         return reader.get_data(0)

~\anaconda3\lib\site-packages\imageio\core\functions.py in get_reader(uri, format, mode, **kwargs)
    180     if format is None:
    181         raise ValueError(
--> 182             "Could not find a format to read the specified file " "in mode %r" % mode
    183         )
    184 

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

您的目录中很可能有 non-image 个文件。

  • 您要么将 non-image 个文件移动到另一个目录,要么您需要指定图像扩展名。

例如:使用glob获取所有png扩展文件。

import matplotlib.pyplot as plt
from skimage.color import rgb2gray
from skimage.transform import resize
from skimage.io import imread, imshow
import pandas as pd
import numpy as np
import os
from glob import glob
import warnings
warnings.simplefilter('ignore')

path = "C:/Users/IMA/Documents/ML/image/p1/*.png"
p1 = glob(path)
p2 = os.listdir("C:/Users/IMA/Documents/ML/image/p2")

limit = 20
p1_image = [None]*limit
j = 0
for i in p1:
    if j < limit:
        p1_image[j] = imread(i)
        j += 1
    else:
        break