这是我的代码。基本上,我想循环加载D驱动器内文件夹中的所有图像。
from PIL import Image
def loadimages(path):
list=listdir(path)
loadedimages=[]
for image in list:
img=Image.open(path+image)
loadedimages.append(img)
return loadedimages
path= r"D:ACADEMICS8SEMPatternClassificationCBT-1TrainCharacters"
imgs=loadimages(path)
我收到这样的错误
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
imgs=loadimages(path)
File "<pyshell#11>", line 5, in loadimages
img=Image.open(path+image)
File "C:Usersanjana ousephAppDataLocalProgramsPythonPython37libsite-packagesPILImage.py", line 2809, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'D:\ACADEMICS\8SEM\PatternClassification\CBT-1\TrainCharacters1'
如果图像位于 TrainCharacter 文件夹中,则缺少"\"
目前它正在寻找
'D:\ACADEMICS\8SEM\PatternClassification\CBT-1\TrainCharacters1'
您可能需要
'D:\ACADEMICS\8SEM\PatternClassification\CBT-1\TrainCharacters\1'
看来问题是行img=Image.open(path+image)
。 我建议使用内置的 Python 路径操作方法os.path.join
:
from PIL import Image
import os
def loadimages(path):
list=listdir(path)
loadedimages=[]
for image in list:
img=Image.open(os.path.join(path, image))
loadedimages.append(img)
return loadedimages
path= r"D:ACADEMICS8SEMPatternClassificationCBT-1TrainCharacters"
imgs=loadimages(path)