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


本文摘自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实现ping指定ip的示例

Python字典怎么根据值返回键

web自动化测试(三)selenium+beatuifulsoup

Python中tornado下websocket客户端编程的介绍

Python函数之compile()函数

Python线程优先级队列是什么?线程优先级的设置方法有哪些?

Python批量合并有合并单元格的excel文件详解_Python

Python安装的图文教程分享

Python之post登录测试

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...