mnist样本字库嘚图片转换:
# coding=utf-8
import numpy as np
import struct
import matplotlib.pyplot as plt
from PIL import Image
#/home/sys/data/mnist/
filename = 't10k-images-idx3-ubyte'
binfile = open(filename,'rb')
buf = binfile.read()
index = 0
magic,numImages,numRows,numColumns = struct.unpack_from('>IIII',buf,index)
index += struct.calcsize('>IIII')
for image in range(0,numImages):
im = struct.unpack_from('>784B',buf,index) #读一个图片
index +=struct.calcsize('>784B')
im = np.array(im,dtype = 'uint8')
im = im.reshape(28,28) #像素大小 #
#fig = plt.figure()
# plotwindow = fig.add_subplot(111)
# plt.imshow(im,cmap = 'gray')
# plt.show()
im = Image.fromarray(im)
im.save('mnist_test/train_%s.bmp'%image,'bmp') #保存转换后的图片格式