FileNotFoundError - 从 WindowsPath 对象打开图像时遇到问题
FileNotFoundError - Trouble opening an image from WindowsPath object
遍历此列表和reading/opening 图像没有错误,您可以看到元组中的每个项目(索引 0)都是 pathlib 模块的 WindowsPath 对象。所以,这有效:
遍历此列表无效,无法找到图像,您可以看到列表中的每一项(索引0)都是一个WindowsPath,指向pathlib模块的WindowsPath对象。我希望这个列表与上图中的列表相同:
对我来说,它似乎试图打开一个名为“[WindowsPath('dir1/img1.png')]”的不存在的文件,而不是名为“dir1/img1”的现有文件.png。这似乎源于调用函数 random_list() 然后将该函数的输出附加到列表中。
见下面的代码:
def random_list():
#set empty list
combo = []
#mapping images to probabilities
for d, p in zip(layers, probabilities):
#random choice of image given the probability
ch = choices(d, p)
#add image path to list
combo.append(ch)
#return list in order to add more images, total of 7 loops
return combo
'''
Main method
'''
#makes a list of all .png images in the given directories
layers = [list(Path(directory).glob("*.png")) for directory in ("dir1/", "dir2/", "dir3/", "dir4/", "dir5/", "dir6/", "dir7/")]
#list of probabilities for each image
probabilities =
[ [0.17, 0.11, 0.15, 0.085, 0.05, 0.235, 0.2],
[0.075, 0.6, 0.3, 0.025],
[0.075, 0.3, 0.6, 0.025],
[0.3, 0.075, 0.6, 0.025],
[0.55, 0.35, 0.1],
[0.1, 0.55, 0.35],
[0.1, 0.35, 0.55] ]
#setting empty list
combinations = []
#while combinations is less than 1,111 items:
while len(combinations) < 1111:
#get random list of traits
a = random_list()
#add to list if unique
if a not in combinations:
combinations.append(a)
#calling iterative function which layers images from directories and saves them
generate(layers)
错误:
File "E:\myDir\subDir\imageGeneratorTest.py", line 22, in generate
layer = Image.open(str(path), "r")
File "C:\Program Files\pkgs\PIL\Image.py", line 2968, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: "[WindowsPath('dir1/img1.png')]"
所以,这对我有用:
while len(genList) < 1111:
a = random_list()
#add to list if unique
if a not in genList:
genList.append(a)
combinations = list([str(c) for c in lst] for lst in genList)
然后用这一行从我的迭代函数中的字符串中取出垃圾:
path = (path.replace("[WindowsPath('", "")).replace("')]", "")
遍历此列表和reading/opening 图像没有错误,您可以看到元组中的每个项目(索引 0)都是 pathlib 模块的 WindowsPath 对象。所以,这有效:
遍历此列表无效,无法找到图像,您可以看到列表中的每一项(索引0)都是一个WindowsPath,指向pathlib模块的WindowsPath对象。我希望这个列表与上图中的列表相同:
对我来说,它似乎试图打开一个名为“[WindowsPath('dir1/img1.png')]”的不存在的文件,而不是名为“dir1/img1”的现有文件.png。这似乎源于调用函数 random_list() 然后将该函数的输出附加到列表中。
见下面的代码:
def random_list():
#set empty list
combo = []
#mapping images to probabilities
for d, p in zip(layers, probabilities):
#random choice of image given the probability
ch = choices(d, p)
#add image path to list
combo.append(ch)
#return list in order to add more images, total of 7 loops
return combo
'''
Main method
'''
#makes a list of all .png images in the given directories
layers = [list(Path(directory).glob("*.png")) for directory in ("dir1/", "dir2/", "dir3/", "dir4/", "dir5/", "dir6/", "dir7/")]
#list of probabilities for each image
probabilities =
[ [0.17, 0.11, 0.15, 0.085, 0.05, 0.235, 0.2],
[0.075, 0.6, 0.3, 0.025],
[0.075, 0.3, 0.6, 0.025],
[0.3, 0.075, 0.6, 0.025],
[0.55, 0.35, 0.1],
[0.1, 0.55, 0.35],
[0.1, 0.35, 0.55] ]
#setting empty list
combinations = []
#while combinations is less than 1,111 items:
while len(combinations) < 1111:
#get random list of traits
a = random_list()
#add to list if unique
if a not in combinations:
combinations.append(a)
#calling iterative function which layers images from directories and saves them
generate(layers)
错误:
File "E:\myDir\subDir\imageGeneratorTest.py", line 22, in generate
layer = Image.open(str(path), "r")
File "C:\Program Files\pkgs\PIL\Image.py", line 2968, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: "[WindowsPath('dir1/img1.png')]"
所以,这对我有用:
while len(genList) < 1111:
a = random_list()
#add to list if unique
if a not in genList:
genList.append(a)
combinations = list([str(c) for c in lst] for lst in genList)
然后用这一行从我的迭代函数中的字符串中取出垃圾:
path = (path.replace("[WindowsPath('", "")).replace("')]", "")