本文实例为大家分享了python或C++读取指定文件夹下的所有图片,供大家参考,具体内容如下
1.python读取指定文件夹下的所有图片路径和图片文件名
import cv2
from os import walk,path
def get_fileNames(rootdir):
data=[]
prefix = []
for root, dirs, files in walk(rootdir, topdown=True):
for name in files:
pre, ending = path.splitext(name)
if ending != ".jpg" and ending != ".jepg" and ending != ".png":
continue
else:
data.append(path.join(root, name))
prefix.append(pre)
return data, prefix
if __name__ == '__main__':
images, preifx = get_fileNames('/home/yasin/target_pics') #得到指定文件夹下的图片,例如.jpg,.jepg或.png等,可根据上述代码更改
for i in range(len(images)):
img = cv2.imread(images[i])
w = img.shape[1]
l = img.shape[0]