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中执行系统命令的方法

如何用Python实现微信消息防撤回

Python实现购物车的简单实例分享

如何查看Python安装路径

Python序列之列表

Python unittest实现api自动化测试_Python

Python中gensim库word2vec的使用

Python安装哪个版本

Python中属性描述符的详细介绍(代码示例)

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




打赏

取消

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

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

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

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

评论

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