2017-02-26 6 views
0

私はtensorflowプロジェクトに取り組んでいましたので、MNISTデータベースからテストイメージを取ってみたいと思います。以下は2D numpyのに元のデータ(?UBYTE)を変換するための私のコードの要旨です:IMGはhereを示します適切な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() 

しかし、私は私が台無しにしておく必要があります理解して、それは通常の数のような何も見えませんどこかにあるが、numpy配列が[784]から[28,28]に再構成されたとき以外はそれを特定できない。すべての手がかりは?

編集:私は代わりにPILのmatplotlibのを使用するのであれば、明らかにそれが正常に動作します:

+0

の可能性のある重複[TensorFlow - MNISTデータセットから表示画像(http://stackoverflow.com/questions/38308378/tensorflow-show-image-from-mnist-dataset) –

+0

Iその投稿を読んでください。しかし、私のイメージがどのように数字のように見えないのかは本当に説明できません。 – dzeng

+0

うん、明らかに、私がPILの代わりにmatplotlibを使うと、画像が正しく表示されます...なぜなのでしょうか?とにかく私はmatoplotlibを使用して、お母さんに – dzeng

答えて

0

乗算データ255によると(uint8 for mode 'L')は、それが動作していnp.uint8に変換します。

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.

関連する問題