项目下的image文件夹里有五张.bmp图像
1 import os
2 import cv2
3
4 def get_img_list(dir, firelist, ext=None):
5 newdir = dir
6 if os.path.isfile(dir): # 如果是文件
7 if ext is None:
8 firelist.append(dir)
9 elif ext in dir[-3:]:
10 firelist.append(dir)
11 elif os.path.isdir(dir): # 如果是目录
12 for s in os.listdir(dir):
13 newdir = os.path.join(dir, s)
14 get_img_list(newdir, firelist, ext)
15
16 return firelist
17
18 def read_img():
19 image_path = './image'
20 imglist = get_img_list(image_path, [], 'bmp')
21 imgall = []
22 for imgpath in imglist:
23 # print(imgpath)
24 imaname = os.path.split(imgpath)[1] # 分离文件路径和文件名后获取文件名(包括了后缀名)
25 # print(imaname)
26 img = cv2.imread(imgpath, cv2.IMREAD_COLOR)
27 imgall.append(img)
28 cv2.namedWindow(imaname, cv2.WINDOW_AUTOSIZE)
29 cv2.imshow(imaname, img)
30 print(imaname, img.shape)
31 cv2.waitKey(0)
32
33 return imgall
34
35 if __name__ == '__main__':
36 imgall = read_img()
37 print(imgall.__len__())
最后图片显示