您的位置:首页 > 编程语言 > Python开发

Python读入mnist二进制图像文件并显示

2019-03-01 10:56 302 查看

图像文件是自己仿照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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: