如何打开从 MNIST 数据库中提取的图像

How do you open extracted images from the MNIST database

基本上,我试图从 MNIST 数据集中获取图像,然后在我的计算机上显示它。问题是,当我尝试打开单个图像(使用 pillow Image.open() 函数)时,它说它不能 'read' 它。我不知道这是一个不起作用的东西还是全部。真的,我只是在玩新东西。

我试过使用 'tensorflow.examples.tutorials.mnist',但它总是出错,我不知道为什么。然后我决定我应该只下载 MNIST 数据并打开它,现在它说它不能 'read' 'numpy.ndarray'.

from PIL import Image
from tensorflow.contrib.learn.python.learn.datasets.mnist import extract_images, extract_labels

with open('train-images-idx3-ubyte (2).gz', 'rb') as f:
  train_images = extract_images(f)
with open('train-labels-idx1-ubyte (1).gz', 'rb') as f:
  train_labels = extract_labels(f)

with open('t10k-images-idx3-ubyte.gz', 'rb') as f:
  test_images = extract_images(f)
with open('t10k-labels-idx1-ubyte.gz', 'rb') as f:
  test_labels = extract_labels(f)

myImage = Image.open(train_images[0])
myImage.show()

我希望它能打开文件,但它只是显示有关打开的错误 train_images[0]

没关系,我找到答案了。

您需要做的是使用 np.reshape() 更改数据的形状,然后使用 Image.fromarray() 而不是 Image.open()。 例如:

MyImage = train_images[0]
MyImage = MyImage.reshape(28, 28)
MyImage = Image.fromarray(MyImage)