编程语言
首页 > 编程语言> > python 读取文件夹下的图片并显示

python 读取文件夹下的图片并显示

作者:互联网

项目下的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     print(imglist.__len__())
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         cv2.namedWindow(imaname, cv2.WINDOW_AUTOSIZE)
28         cv2.imshow(imaname, img)
29         print(img.shape)
30     cv2.waitKey(0)
31 
32     return imglist
33 
34 if __name__ == '__main__':
35     imglist = read_img()
36     print(imglist.__len__())

最后图片显示

 

标签:__,读取,img,python,imglist,cv2,文件夹,path,dir
来源: https://www.cnblogs.com/myblog-wll/p/14066832.html