如果图像位于单个目录中,则下面的代码可以正常工作。
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;文件夹有很多子文件夹和子文件夹都有图像文件,如何处理呢?
答案 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)
我希望这会有所帮助。