尝试为特定文件名绘制图像
Trying to plot images for specific file names
我正在尝试使用 matplotlib 从我的文件夹中打印一堆图片,但是我不确定如何修复此错误。
基本上,我正在尝试获取具有“_ 0 _”或“_ 1 _”模式的特定文件,但它给了我一个错误并且不确定如何修复它。
我放了一张我正在尝试复制的代码示例的屏幕截图,如果您有任何问题,请告诉我。欢迎任何帮助!
import os
import matplotlib.pyplot as plt
from matplotlib.image import imread
#folder = '/content/drive/MyDrive/Colab Notebooks/data/dogs-vs-cats/train/'
plt.figure(figsize=(10,10))
for i in range(10):
plt.subplot(4,5,i+1)
filename = glob.glob(os.path.join(folder,'*_1_*.jpg'))
#for filename in folder:
#filename = glob.glob(folder +'*_1_*.jpg')
#filename = glob.glob('/content/drive/MyDrive/Colab Notebooks/train_val'/'*_1_*.jpg')
#filename = folder + '*_1_*.jpg'
if not os.path.exists(filename):
#if not os.path.exists(filename):
print ('No such file:'+ filename)
image = imread(filename)
plt.imshow(image)
for i in range(10):
plt.subplot(4,5,i+11)
plt.subplot(4,5,i+11)
plt.subplot(4,5,i+11)
filename = glob.glob(os.path.join(folder,'*_0_*.jpg'))
#filename = glob.glob('/content/drive/MyDrive/Colab Notebooks/train_val'/'*_0_*.jpg')
#filename = folder + ' *_0_*.jpg'
image = imread(filename)
plt.imshow(image)
plt.show()
错误信息:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-21-8c96486bb2b7> in <module>()
11 #filename = glob.glob('/content/drive/MyDrive/Colab Notebooks/train_val'/'*_1_*.jpg')
12 #filename = folder + '*_1_*.jpg'
---> 13 if not os.path.exists(filename):
14 #if not os.path.exists(filename):
15 print ('No such file:'+ filename)
/usr/lib/python3.7/genericpath.py in exists(path)
17 """Test whether a path exists. Returns False for broken symbolic links"""
18 try:
---> 19 os.stat(path)
20 except OSError:
21 return False
TypeError: stat: path should be string, bytes, os.PathLike or integer, not list
我想要完成的事情:
glob.glob()
函数returns一个列表,错误信息告诉你
TypeError: stat: path should be string, bytes, os.PathLike or integer, not list
你的循环应该是这样的,而不是:
for i in range(10):
plt.subplot(4, 5, i+1)
filelist = glob.glob(os.path.join(folder, '*_1_*.jpg'))
to_process = []
for entry in filelist:
if not os.path.exists(entry):
print ("File {entry} does not exist".format(entry=entry))
continue
else:
to_process.append(entry)
for entry in to_process:
# ... other desired processing
我正在尝试使用 matplotlib 从我的文件夹中打印一堆图片,但是我不确定如何修复此错误。
基本上,我正在尝试获取具有“_ 0 _”或“_ 1 _”模式的特定文件,但它给了我一个错误并且不确定如何修复它。
我放了一张我正在尝试复制的代码示例的屏幕截图,如果您有任何问题,请告诉我。欢迎任何帮助!
import os
import matplotlib.pyplot as plt
from matplotlib.image import imread
#folder = '/content/drive/MyDrive/Colab Notebooks/data/dogs-vs-cats/train/'
plt.figure(figsize=(10,10))
for i in range(10):
plt.subplot(4,5,i+1)
filename = glob.glob(os.path.join(folder,'*_1_*.jpg'))
#for filename in folder:
#filename = glob.glob(folder +'*_1_*.jpg')
#filename = glob.glob('/content/drive/MyDrive/Colab Notebooks/train_val'/'*_1_*.jpg')
#filename = folder + '*_1_*.jpg'
if not os.path.exists(filename):
#if not os.path.exists(filename):
print ('No such file:'+ filename)
image = imread(filename)
plt.imshow(image)
for i in range(10):
plt.subplot(4,5,i+11)
plt.subplot(4,5,i+11)
plt.subplot(4,5,i+11)
filename = glob.glob(os.path.join(folder,'*_0_*.jpg'))
#filename = glob.glob('/content/drive/MyDrive/Colab Notebooks/train_val'/'*_0_*.jpg')
#filename = folder + ' *_0_*.jpg'
image = imread(filename)
plt.imshow(image)
plt.show()
错误信息:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-21-8c96486bb2b7> in <module>()
11 #filename = glob.glob('/content/drive/MyDrive/Colab Notebooks/train_val'/'*_1_*.jpg')
12 #filename = folder + '*_1_*.jpg'
---> 13 if not os.path.exists(filename):
14 #if not os.path.exists(filename):
15 print ('No such file:'+ filename)
/usr/lib/python3.7/genericpath.py in exists(path)
17 """Test whether a path exists. Returns False for broken symbolic links"""
18 try:
---> 19 os.stat(path)
20 except OSError:
21 return False
TypeError: stat: path should be string, bytes, os.PathLike or integer, not list
我想要完成的事情:
glob.glob()
函数returns一个列表,错误信息告诉你
TypeError: stat: path should be string, bytes, os.PathLike or integer, not list
你的循环应该是这样的,而不是:
for i in range(10):
plt.subplot(4, 5, i+1)
filelist = glob.glob(os.path.join(folder, '*_1_*.jpg'))
to_process = []
for entry in filelist:
if not os.path.exists(entry):
print ("File {entry} does not exist".format(entry=entry))
continue
else:
to_process.append(entry)
for entry in to_process:
# ... other desired processing