TeigeC
TeigeC

Reputation: 96

How do you open extracted images from the MNIST database

Basically, I'm trying to get an image from the MNIST dataset and then show it on my computer. The problem is that when I try to open a single image (using the pillow Image.open() function), it says it can't 'read' it. I can't tell if it's a single thing that's not working or all of it. Really, I'm just messing around with new stuff.

I tried using the 'tensorflow.examples.tutorials.mnist', but it just keeps mucking up, I don't know why. Then I decided that I should just download the MNIST data and open that and now it's saying it can't '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()

I expected it to open the file but it just shows up with an error about opening train_images[0]

Upvotes: 1

Views: 1525

Answers (1)

TeigeC
TeigeC

Reputation: 96

Nevermind, I found out the answer.

What you needed to do was change the shape of the data using np.reshape() and then use Image.fromarray() instead of Image.open(). For example:

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

Upvotes: 1

Related Questions