遍历目录和子目录中的所有图像,以执行单个图像的平均强度

时间:2018-04-04 07:26:47

标签: python

如果图像位于单个目录中,则下面的代码可以正常工作。

from skimage import io, img_as_float
    import matplotlib.image as mpimg
    import os
    import pandas as pd    

def load_images(folder):
    images = []
    data = []
    for filename in os.listdir(folder):
        img = mpimg.imread(os.path.join(folder, filename))
        img = img_as_float(img)
        if img is not None:
            images.append(img)
        if(np.mean(img) < 0.1):
            print filename
            data.append((folder, filename))
            df = pd.DataFrame(data, columns=['Folder', 'File'])
    return df

df = load_images('C:/Data/Testing')
df.to_csv('final.csv')

如果我的C:/ Data / Testing&#39;文件夹有很多子文件夹和子文件夹都有图像文件,如何处理呢?

1 个答案:

答案 0 :(得分:2)

如果有人正在查看此问题,请回答以下问题:

from skimage import io, img_as_float
import matplotlib.image as mpimg
import os
import pandas as pd

def load_images(n):
    images = []
    data = []
    for dirs in n:
        for filename in os.listdir(dirs):
            img = mpimg.imread(os.path.join(dirs, filename))
            img = img_as_float(img)
            if img is not None:
                images.append(img)
            if(np.mean(img) < 0.1):
                print filename
                data.append((dirs, filename))
                df = pd.DataFrame(data, columns=['Folder', 'File'])
    return df

from glob import glob
folder = glob("C:/Data/Testing/*/")
df = load_images(folder)

我希望这会有所帮助。