本文摘自php中文网,作者不言,侵删。
下面为大家分享一篇Python 读取指定文件夹下的所有图像方法,具有很好的参考价值,希望对大家有所帮助。一起过来看看吧(1)数据准备
数据集介绍:
数据集中存放的是1223幅图像,其中756个负样本(图像名称为0.1~0.756),458个正样本(图像名称为1.1~1.458),其中:"."前的标号为样本标签,"."后的标号为样本序号
(2)利用python读取文件夹中所有图像
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | '' '
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)调用方式
1 2 3 | craterDir = "./data/CraterImg/Adjust/"
foldName = "East_CraterAdjust12"
data, label = load_Img(craterDir,foldName)
|
相关推荐:
python读取csv文件并把文件放入一个list中的实例讲解
python实现对文件中图片生成带标签的txt文件方法
以上就是Python 读取指定文件夹下的所有图像方法的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
Python基础知识之二:网络通信数据传输
Python怎么调试?
Python中正则表达式的详细介绍
Python怎么调用自定义函数
Python详解os模块和shutil模块
Python如何切换线程
Python中常见数据库有哪些
Python中线程与进程的区别与优劣
Python实现有序字典方法示例
Python如何模拟实现生产者消费者模式的代码案例
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » Python 读取指定文件夹下的所有图像方法