下面为大家分享一篇python 读取指定文件夹下的所有图像方法,具有很好的参考价值,希望对大家有所帮助。一起过来看看吧
(1)数据准备
数据集介绍:
数据集中存放的是1223幅图像,其中756个负样本(图像名称为0.1~0.756),458个正样本(图像名称为1.1~1.458),其中:"."前的标号为样本标签,"."后的标号为样本序号
(2)利用python读取文件夹中所有图像
立即学习“Python免费学习笔记(深入)”;
'''
Load the image files form the folder
input:
imgDir: the direction of the folder
imgName:the name of the folder
output:
data:the data of the dataset
label:the label of the datset
'''
def load_Img(imgDir,imgFoldName):
imgs = os.listdir(imgDir+imgFoldName)
imgNum = len(imgs)
data = np.empty((imgNum,1,12,12),dtype="float32")
label = np.empty((imgNum,),dtype="uint8")
for i in range (imgNum):
img = Image.open(imgDir+imgFoldName+"/"+imgs[i])
arr = np.asarray(img,dtype="float32")
data[i,:,:,:] = arr
label[i] = int(imgs[i].split('.')[0])
return data,label这里得到的data和label都是ndarray数据
data: (1223,1,12,12)

label:(1223,)

注:nddary数据类型是numpy提供的一个数据类型,即N-dimensional array,它弥补了python中array不支持多维的缺陷
(3)调用方式
craterDir = "./data/CraterImg/Adjust/" foldName = "East_CraterAdjust12" data, label = load_Img(craterDir,foldName)
相关推荐:
python读取csv文件并把文件放入一个list中的实例讲解
以上就是Python 读取指定文件夹下的所有图像方法的详细内容,更多请关注php中文网其它相关文章!
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号