读取MNIST数据集并显示数据集图片 完全解析

# coding: utf-8
import sys, os
sys.path.append(os.pardir)  # 为了导入父目录的文件而进行的设定
import numpy as np
from dataset.mnist import load_mnist
from PIL import Image
#unit8(无符号的整数,unit8是0~255
def img_show(img):
    pil_img = Image.fromarray(np.uint8(img))#Image.fromarray图像数据转换为PIL数据对象
    pil_img.show()#显示图片

(x_train, t_train), (x_test, t_test) = load_mnist(flatten=True, normalize=False)
#load_mnist读取MNIST数据返回(训练图像,训练标签),(测试图像,测试标签)
img = x_train[0]#训练图像赋给img
label = t_train[0]#训练标签赋给label
print(label)  # 5

print(img.shape)  # (784,)
img = img.reshape(28, 28)  # 把图像的形状变为原来的尺寸
print(img.shape)  # (28, 28)

img_show(img)#函数

5
(784,)
(28, 28)
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_33595571/article/details/83515308