用python对minist数据集进行读取并保存成图片
2016-09-27 10:35
453 查看
记忆力差的孩纸得勤做笔记!
参考博客http://www.cnblogs.com/x1957/archive/2012/06/02/2531503.html,最近想要拿minist数据集练练手,想要将minsit数据集转换成图片格式,参考了上面博客后,有了下面这个demo,给大家分享下!好了,多的不说直接上代码!
下面是读取label的demo,最后label会在label.txt文件夹中以列的形式存在。
参考博客http://www.cnblogs.com/x1957/archive/2012/06/02/2531503.html,最近想要拿minist数据集练练手,想要将minsit数据集转换成图片格式,参考了上面博客后,有了下面这个demo,给大家分享下!好了,多的不说直接上代码!
#coding:utf-8 import numpy as np import struct import matplotlib.pyplot as plt from scipy.misc import imsave filename = 'train-images.idx3-ubyte' binfile = open(filename , 'rb') buf = binfile.read() index = 0 #'>IIII'使用大端法读取四个unsigned int32 magic, numImages , numRows , numColumns = struct.unpack_from('>IIII' , buf , index) index += struct.calcsize('>IIII') # 输出大端数 print magic print numImages print numRows print numColumns for i in range(60000): name = str(i) + ".jpg" # upack_from从流中截取784位数据(图片像素值) im = struct.unpack_from('>784B' ,buf, index) index += struct.calcsize('>784B') im = np.array(im) im = im.reshape(28,28) imsave(name, im) # fig = plt.figure() # plotwindow = fig.add_subplot(111) # plt.imshow(im , cmap='gray') # plt.show()
下面是读取label的demo,最后label会在label.txt文件夹中以列的形式存在。
#coding:utf-8 import numpy as np import struct # import matplotlib.pyplot as plt # from scipy.misc import imsave #filename = 'dat//train-images.idx3-ubyte' filename = 'dat//train-labels.idx1-ubyte' binfile = open(filename , 'rb') buf = binfile.read() f1 = open('label.txt', 'w') index = 0 #'>IIII'使用大端法读取两个unsigned int32 magic, numLabels = struct.unpack_from('>II' , buf , index) index += struct.calcsize('>II') # 输出大端数 print magic print numLabels # for i in range(numLabels): for i in range(numLabels): numtemp = struct.unpack_from('1B' ,buf, index) # numtemp 为tuple类型,读取其数值 num = numtemp[0] # 存入label.txt文件中 f1.write(str(num)) f1.write('\n') index += struct.calcsize('1B') print num # fig = plt.figure() # plotwindow = fig.add_subplot(111) # plt.imshow(im , cmap='gray') # plt.show()打完收工!demo下载地址:点击打开链接
相关文章推荐
- 利用Python PIL、cPickle将图片读取和保存为pkl格式文件
- Python中使用pickle Image等进行图片序列化及读取恢复显示图片
- (超详细)读取mnist数据集并保存成图片
- Python 读取图片文件为矩阵和保存矩阵为图片的方法
- Python---对html文件内容进行搜索取出特定URL地址字符串,保存成列表,并使用每个url下载图片,并保存到硬盘上,使用正则re
- 在sklearn上读取人脸数据集保存图片到本地
- python读取,显示,保存mnist图片
- Python 读取文件夹将里面的图片处理成想要的大小并保存在个指定位置
- python 读取、保存、二值化、灰度化图片+opencv处理图片的方法
- TypeError: cannot concatenate 'str' and 'list' objects和Python读取和保存图片
- Python配合BeautifulSoup读取网络图片并保存在本地
- Python之BeautifulSoup学习之三 读取本地html文件,并将其中图片保存下来
- python 读取文件夹下的图片进行处理
- Python中读取,显示,保存图片的方法
- python从网络读取图片并直接进行处理的方法
- python读取鸢尾花数据集并进行训练测试集分割
- Java将图片作为二进制进行保存和读取
- Python读取多个txt文件并进行保存
- python 读取、保存、二值化、灰度化图片+opencv处理图片的方法
- Python---对html文件内容进行搜索取出特定URL地址字符串,保存成列表,并使用每个url下载图片,并保存到硬盘上,使用bs4,beautifulsoup模块