使用Scikit io模块导入图像文件夹



我正在尝试使用scikit图像包学习图像处理。我知道我最终需要导入带有大量图像的文件夹。我首先单独指定每个文件的路径,这很好,我能够生成我希望的数组。

import numpy as np
import matplotlib as plt
from skimage import io
from skimage import viewer
fID1 = 'C:/Users/keand/Documents/Strader Lab/HJ pictures/PictureB.jpg'
fID2 = 'C:/Users/keand/Documents/Strader Lab/HJ pictures/PictureA.jpg'
root1 = io.imread(fID1)
root2 = io.imread(fID2)

然后,我尝试使用scikit文档中的示例来加载一个文件夹。

>>> import os
>>> from natsort import natsorted, ns
>>> from skimage import io
>>> list_files = os.listdir('.')
>>> list_files
['01.png', '010.png', '0101.png', '0190.png', '02.png']
>>> list_files = natsorted(list_files)
>>> list_files
['01.png', '02.png', '010.png', '0101.png', '0190.png']
>>> image_list = []
>>> for filename in list_files:
...   image_list.append(io.imread(filename))

这是我的消遣:

import os
from natsort import natsorted,ns
from skimage import io
from skimage.io import imread
listFiles = os.listdir('C:/Users/keand/Documents/Strader Lab/HJ pictures')
# listFiles = os.listdir('./HJ pictures')
print(listFiles)
listFiles = natsorted(listFiles)
print(listFiles)
imageList = []
for filename in listFiles:
imageList.append(io.imread(filename))

当我运行这个时,我得到以下内容:

runfile('C:/Users/keand/Documents/Strader Lab/ImageProcessing.py', wdir='C:/Users/keand/Documents/Strader Lab')
['PictureA.jpg', 'PictureB.jpg']
['PictureA.jpg', 'PictureB.jpg']
Traceback (most recent call last):
File "C:UserskeandDocumentsStrader LabImageProcessing.py", line 31, in <module>
imageList.append(io.imread(filename))
File "C:Userskeandanaconda3libsite-packagesskimageio_io.py", line 48, in imread
img = call_plugin('imread', fname, plugin=plugin, **plugin_args)
File "C:Userskeandanaconda3libsite-packagesskimageiomanage_plugins.py", line 207, in call_plugin
return func(*args, **kwargs)
File "C:Userskeandanaconda3libsite-packagesskimageio_pluginsimageio_plugin.py", line 10, in imread
return np.asarray(imageio_imread(*args, **kwargs))
File "C:Userskeandanaconda3libsite-packagesimageiocorefunctions.py", line 265, in imread
reader = read(uri, format, "i", **kwargs)
File "C:Userskeandanaconda3libsite-packagesimageiocorefunctions.py", line 172, in get_reader
request = Request(uri, "r" + mode, **kwargs)
File "C:Userskeandanaconda3libsite-packagesimageiocorerequest.py", line 124, in __init__
self._parse_uri(uri)
File "C:Userskeandanaconda3libsite-packagesimageiocorerequest.py", line 260, in _parse_uri
raise FileNotFoundError("No such file: '%s'" % fn)
FileNotFoundError: No such file: 'C:UserskeandDocumentsStrader LabPictureA.jpg'

有人能帮我弄清楚为什么当FileNoFoundError识别出图像的名称时,我会得到它吗?

以下是我尝试过的一些故障排除步骤。

  1. 重命名文件
  2. 确保python脚本和文件位于同一目录中
  3. 创建列表后打印列表文件
  4. 将+name添加到行的末尾,首先定义listFiles
  5. 导入numpy和matplotlib以防存在任何依赖关系

使用io模块的io.imread_collection函数要容易得多。这将创建一个图像集合,这些图像存储为nd数组。

from skimage.io import imread
from skimage.io import imread_collection
dirName = 'C:/Users/keand/Documents/Strader_Lab/Image_Processing/HJ_Pictures/*.jpg'
collection = imread_collection(dirName)

检查错误消息,您试图加载的文件路径不存在。

图像不在您的工作目录中。您必须按文件路径而不是文件名加载它们。

import os
scr = 'C:/Users/keand/Documents/Strader Lab/HJ pictures'
listFiles = [os.path.join(src, f) for f in os.listdir(src)]
print(listFiles)

我更喜欢使用pathlib库:

from pathlib import Path 
scr = Path('C:/Users/keand/Documents/Strader Lab/HJ pictures')
listFiles = [f.resolve() for f in src.glob("*.jpg")]
print(listFiles)

相关内容

  • 没有找到相关文章