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

yipeiwu_com6年前Python基础

(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)

label:(1223,)

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

(3)调用方式

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

以上这篇Python 读取指定文件夹下的所有图像方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python使用matplotlib填充图形指定区域代码示例

Python使用matplotlib填充图形指定区域代码示例

本文代码重点在于演示Python扩展库matplotlib.pyplot中fill_between()函数的用法。 import numpy as np import matplot...

Python xlrd读取excel日期类型的2种方法

有个excle表格需要做一些过滤然后写入数据库中,但是日期类型的cell取出来是个数字,于是查询了下解决的办法。 基本的代码结构 复制代码 代码如下: data = xlrd.open_...

详解Django-channels 实现WebSocket实例

引入 先安装三个模块 pip install channels pip install channels_redis pip install pywin32 创建一个Dja...

python paramiko模块学习分享

paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接。paramiko支持Linux, Solaris, BSD, MacOS...

Python中关键字nonlocal和global的声明与解析

一、Python中global与nonlocal 声明 如下代码 a = 10 def foo(): a = 100 执行foo() 结果 a 还是10 函数中对变...