matplotlib 如何从每个文件夹子图图像?

jckbn6z7  于 2023-06-23  发布在  其他
关注(0)|答案(1)|浏览(75)

我用这些代码列出图像:

for folder in folder_list:
    img = os.path.join(f"{train_dir}/{folder}/00001.png")
    print(img)

输出为:
Fish/train/Black Sea Sprat/00001.png/Fish/train/Gilt-Head Bream/00001.png Fish/train/Horse Mackerel/00001.png Fish/train/Red Sea Bream/00001.png Fish/train/Sea Bass/00001.png Fish/train/Shrimp/00001.png/Fish/train/Striped Red Mullet/00001.png下载/Fish/train/Trout/00001.有9张图片。

ruarlubt

ruarlubt1#

您只是打印图像文件名。您需要enumerate图像列表并使用plt.sublot逐个绘制

import os
import matplotlib.pyplot as plt
import PIL
files=[] #empty list
rows = 3 #Number of rows u need

for folder in folder_list:
    img = os.path.join(f"{train_dir}/{folder}/00001.png")
    files.append(img)
   
for num, x in enumerate(files):
    img = PIL.Image.open(x)
    plt.subplot(rows,6,num+1)
    plt.title(x.split('.')[0])
    plt.axis('off')
    plt.imshow(img)

相关问题