0

Hey guys so I've been working on a tensorflow project and I want to take a took at the test images from the MNIST database. Below is the gist of my code for converting the original data(ubyte?) into 2d numpy:

from PIL import Image
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot = True)
def gen_image(arr):
   two_d = np.reshape(arr, (28, 28))
   img = Image.fromarray(two_d, 'L')
   return img

batch_xs, batch_ys = mnist.test.next_batch(1)
gen_image(batch_xs[0]).show()

However when the img gets shown here, it looks nothing like a normal number so I figure I must have messed up somewhere, but can't pinpoint it other than when the numpy array gets reshaped to [28, 28] from [784]. Any clues?

EDIT: So apparently if I use matplotlib instead of PIL it works fine:

dzeng
  • 1
  • 2
  • Possible duplicate of [TensorFlow - Show image from MNIST DataSet](http://stackoverflow.com/questions/38308378/tensorflow-show-image-from-mnist-dataset) – Andrey Sobolev Feb 26 '17 at 17:07
  • I read that post but it doesn't really explain how my image doesn't look like a number? – dzeng Feb 26 '17 at 17:11
  • Hmm so apparently if I use matplotlib instead of PIL the images will show correctly... I wonder why. Anyways I'll use matplotlib for the momen – dzeng Feb 26 '17 at 17:28

1 Answers1

2

Multiply the data by 255 and convert to np.uint8 (uint8 for mode 'L') have it work.

def gen_image(arr):
    two_d = (np.reshape(arr, (28, 28)) * 255).astype(np.uint8)
    img = Image.fromarray(two_d, 'L')
    return img

This answer helps.

Community
  • 1
  • 1
weitang114
  • 1,177
  • 8
  • 14