2017-02-26 81 views
0

嘿,大家好,我一直在做一個tensorflow項目,我想從MNIST數據庫的測試圖像中拿走。下面是我的代碼原始數據(UBYTE?)轉換成2D numpy的要點:如何生成適當的MNIST圖像?

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() 

然而,當IMG獲得展示here,它看上去一點也不像一個正常的數字,所以我想我一定是搞砸了某處,但除了當[numpy array]從[784]重構爲[28,28]時無法查明它。任何線索?

編輯:由255

+0

的可能的複製[TensorFlow - 顯示從數據集MNIST圖像](http://stackoverflow.com/questions/38308378/tensorflow-show-image-from-mnist-dataset) –

+0

我閱讀這篇文章,但它並沒有真正解釋我的圖像看起來不像一個數字? – dzeng

+0

嗯,很明顯,如果我使用matplotlib而不是PIL,圖像將顯示正確...我不知道爲什麼。無論如何,我會使用matplotlib爲momen – dzeng

回答

0

乘的數據,並轉換爲np.uint8 (uint8 for mode 'L')它的工作:因此很明顯,如果我用PIL的matplotlib,而不是它工作正常。

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.