opencv-python递归遍历文件夹下所有图片文件

简介:递归遍历一个文件夹下的所有图片。

  • 函数原型
import os

def getAllImage(folderPath,imageList):
    extend_name = ["jpg","jpeg","png","bmp"]
    if os.path.isfile(folderPath):
        if item.split('.')[-1] in extend_name:
            imageList.append(filePath)
        return imageList
    for item in os.listdir(folderPath):
        if os.path.isdir(os.path.join(folderPath,item)):
            subFolderPath = os.path.join(folderPath, item)
            getAllImage(subFolderPath, imageList)
        else:
            
            filePath = os.path.join(folderPath,item)
            if os.path.isfile(filePath):
                if item.split('.')[-1] in extend_name:
                    imageList.append(filePath)
    return imageList
    

path = r"K:\imageData\thuaaa\prjimg"
imageList = []
getAllImage(path,imageList)
print("len(imageList): ",len(imageList))
print("imageList[:5]: ",imageList[:5])
  • 输出
len(imageList):  2241
imageList[:5]:  ['K:\\imageData\\thuaaa\\prjimg\\D180E018Q-HW01-02-正面@\\D180E018Q-HW01-02-正面\\000000.bmp', 'K:\\imageData\\thuaaa\\prjimg\\D180E018Q-HW01-02-正面@\\D180E018Q-HW01-02-正面\\000001.bmp', 'K:\\imageData\\thuaaa\\prjimg\\D180E018Q-HW01-02-正面@\\D180E018Q-HW01-02-正面\\000002.bmp', 'K:\\imageData\\thuaaa\\prjimg\\D180E018Q-HW01-02-正面@\\D180E018Q-HW01-02-正面\\000003.bmp', 'K:\\imageData\\thuaaa\\prjimg\\D180E018Q-HW01-02-正面@\\D180E018Q-HW01-02-正面\\000004.bmp']

你可能感兴趣的:(opencv-python递归遍历文件夹下所有图片文件)