Reputation: 1
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:
Upvotes: 0
Views: 2744
Reputation: 1303
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
Upvotes: 2