图像文件是自己仿照mnist格式制作,每张图像大小为128*128
import struct
import matplotlib.pyplot as plt
import numpy as np
#读入整个训练数据集图像
filename = 'train-images-idx3-ubyte'
binfile = open(filename, 'rb')
buf = binfile.read()
#读取头四个32bit的interger
index = 0
magic, numImages, numRows, numColumns = struct.unpack_from('>IIII', buf, index)
index += struct.calcsize('>IIII')
#读取一个图片,16384=128*128
im = struct.unpack_from('>16384B', buf, index)
index += struct.calcsize('>16384B')
im=np.array(im)
im=im.reshape(128,128)
fig = plt.figure()
plotwindow = fig.add_subplot(111)
plt.imshow(im, cmap = 'gray')
plt.show()