如何在tf.decode_image之后显示图像的代码

时间:2017-04-13 15:41:15

标签: python tensorflow

我正试图看看tf如何解码图像,所以我尝试

import tensorflow as tf
image1 = tf.image.decode_png('/usr/src/pycharm-2017.1/bin/pycharm.png')
print(image1.shape)
with tf.Session() as sess:
    img = sess.run(image1)
    print(img.shape, img)

但它会引发错误

InvalidArgumentError (see above for traceback): Invalid PNG header, data size 39
 [[Node: DecodePng = DecodePng[channels=0, dtype=DT_UINT8, _device="/job:localhost/replica:0/task:0/cpu:0"](DecodePng/contents)]]

我也尝试了tf.image.decode_image,但它也没有用。 怎么了 ?我该如何解决? 谢谢你

1 个答案:

答案 0 :(得分:2)

tf.image.decode_png接受一个类型为Tensor的字符串,所以你需要先将Tngorflow读入png,然后再将它传递给函数:

import tensorflow as tf
image1 = tf.image.decode_png(tf.read_file('/usr/src/pycharm-2017.1/bin/pycharm.png'))
print(image1.shape)
with tf.Session() as sess:
    img = sess.run(image1)
    print(img.shape, img)