Python,使用jupyter显示位于子目录中的图像
我正在尝试显示位于不同子目录中的图像 我正在尝试以下代码:Python,使用jupyter显示位于子目录中的图像,python,jupyter-notebook,glob,Python,Jupyter Notebook,Glob,我正在尝试显示位于不同子目录中的图像 我正在尝试以下代码: files = glob.glob('Ensemble/*/*', recursive = True) for file in files: print(file) #This print the path of all the images for j in file.glob("*.jpg"): image = cv2.imread(str(j)) plt.imshow(image)
files = glob.glob('Ensemble/*/*', recursive = True)
for file in files:
print(file) #This print the path of all the images
for j in file.glob("*.jpg"):
image = cv2.imread(str(j))
plt.imshow(image)
plt.show()
输出:
Ensemble\Cercles\Cercle2
Ensemble\Cercles\Cercle3
Ensemble\Cercles\Cercle4
Ensemble\Cercles\Cercle5
Ensemble\Diamants\Diamant2
Ensemble\Diamants\Diamant3
Ensemble\Diamants\Diamant4
Ensemble\Diamants\Diamant5
Ensemble\Hexagones\Hexagone2
Ensemble\Hexagones\Hexagone3
Ensemble\Hexagones\Hexagone4
Ensemble\Hexagones\Hexagone5
Ensemble\Triangles\Triangle2
Ensemble\Triangles\Triangle3
Ensemble\Triangles\Triangle4
Ensemble\Triangles\Triangle5
使用此代码,我可以轻松获得所有图像的路径。但是如何显示图像呢
我有一个错误:
AttributeError Traceback (most recent call last)
<ipython-input-34-c624a5120e94> in <module>
22 #plt.show()
23 #type(image)
---> 24 for j in file.glob("*.jpg"):
25 image = cv2.imread(str(j))
26 plt.imshow(image)
AttributeError: 'str' object has no attribute 'glob'
AttributeError回溯(最近一次调用)
在里面
22#plt.show()
23#类型(图像)
--->24表示文件.glob(“*.jpg”)中的j:
25 image=cv2.imread(str(j))
26 plt.imshow(图片)
AttributeError:“str”对象没有属性“glob”
将.glob(“*.jpg”)文件中j的更改为以下内容:
if file.endswith(“.jpg”):
并将image=cv2.imread(str(j))
更改为以下内容
image = cv2.imread(file)