Python 读取指定文件夹下的所有图像方法


Posted in Python onApril 27, 2018

(1)数据准备

数据集介绍:

数据集中存放的是1223幅图像,其中756个负样本(图像名称为0.1~0.756),458个正样本(图像名称为1.1~1.458),其中:"."前的标号为样本标签,"."后的标号为样本序号

(2)利用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)

Python 读取指定文件夹下的所有图像方法

label:(1223,)

Python 读取指定文件夹下的所有图像方法

注:nddary数据类型是numpy提供的一个数据类型,即N-dimensional array,它弥补了python中array不支持多维的缺陷

(3)调用方式

craterDir = "./data/CraterImg/Adjust/"
foldName = "East_CraterAdjust12"
data, label = load_Img(craterDir,foldName)

以上这篇Python 读取指定文件夹下的所有图像方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python threading多线程编程实例
Sep 18 Python
为python设置socket代理的方法
Jan 14 Python
Python命名空间的本质和加载顺序
Dec 17 Python
pymysql模块的使用(增删改查)详解
Sep 09 Python
python安装gdal的两种方法
Oct 29 Python
使用python模拟高斯分布例子
Dec 09 Python
PyTorch中的padding(边缘填充)操作方式
Jan 03 Python
Django中的AutoField字段使用
May 18 Python
python如何保存文本文件
Jun 07 Python
python用什么编辑器进行项目开发
Jun 17 Python
python之基数排序的实现
Jul 26 Python
浅谈Python中对象是如何被调用的
Apr 06 Python
python sys,os,time模块的使用(包括时间格式的各种转换)
Apr 27 #Python
python读取文件名称生成list的方法
Apr 27 #Python
python实现图书馆研习室自动预约功能
Apr 27 #Python
Python实现去除列表中重复元素的方法小结【4种方法】
Apr 27 #Python
详解python多线程、锁、event事件机制的简单使用
Apr 27 #Python
Python Requests模拟登录实现图书馆座位自动预约
Apr 27 #Python
Python多线程中阻塞(join)与锁(Lock)使用误区解析
Apr 27 #Python
You might like
discuz 首页四格:最新话题+最新回复+热门话题+精华文章插件
2007/08/19 PHP
PHP开发中常用的字符串操作函数
2011/02/08 PHP
php模块memcache和memcached区别分析
2011/06/14 PHP
PHP中SESSION使用中的一点经验总结
2012/03/30 PHP
javascript 点击整页变灰的效果(可做退出效果)。
2008/01/09 Javascript
JQuery困惑—包装集 DOM节点
2009/10/16 Javascript
Ajax 数据请求的简单分析
2011/04/05 Javascript
javascript实现阻止iOS APP中的链接打开Safari浏览器
2014/06/12 Javascript
node.js中格式化数字增加千位符的几种方法
2015/07/03 Javascript
JavaScript File API实现文件上传预览
2016/02/02 Javascript
在AngularJS中使用jQuery的zTree插件的方法
2016/04/21 Javascript
从零学习node.js之express入门(六)
2017/02/25 Javascript
使用D3.js制作图表详解
2017/08/13 Javascript
vue2.0+vuex+localStorage代办事项应用实现详解
2018/05/31 Javascript
你应该了解的JavaScript Array.map()五种用途小结
2018/11/14 Javascript
微信内置浏览器图片查看器的代码实例
2019/10/08 Javascript
解决antd 表单设置默认值initialValue后验证失效的问题
2020/11/02 Javascript
使用Python的Supervisor进行进程监控以及自动启动
2014/05/29 Python
使用Pyrex来扩展和加速Python程序的教程
2015/04/13 Python
Python机器学习之决策树算法
2017/12/22 Python
Python使用修饰器进行异常日志记录操作示例
2019/03/19 Python
python 梯度法求解函数极值的实例
2019/07/10 Python
Python定时任务工具之APScheduler使用方式
2019/07/24 Python
python使用paramiko模块通过ssh2协议对交换机进行配置的方法
2019/07/25 Python
python子线程退出及线程退出控制的代码
2019/10/16 Python
Python Socket多线程并发原理及实现
2020/12/11 Python
python 基于opencv实现高斯平滑
2020/12/18 Python
Marks & Spencer爱尔兰:英国马莎百货
2016/04/20 全球购物
德国黑胶唱片、街头服装及运动鞋网上商店:HHV
2018/08/24 全球购物
公司拓展活动方案
2014/02/13 职场文书
葛优非诚勿扰搞笑征婚台词
2014/03/17 职场文书
竞争上岗演讲稿范文
2014/05/12 职场文书
小学数学课题方案
2014/06/15 职场文书
开展读书活动总结
2014/06/30 职场文书
大专生自我鉴定怎么写
2014/09/16 职场文书
客房领班岗位职责
2015/02/11 职场文书