Tensorflow - 将python.framework.ops.Tensor转换为PNG图像

时间:2017-11-01 13:30:21

标签: tensorflow

是否可以将python.framework.ops.Tensor转换为PNG图像?

操作是:Tensor("layer_1/Tahn:0", shape=(1, 256, 256, 3), dtype=float32)

1 个答案:

答案 0 :(得分:1)

tf.image.encode_png(tf.cast((tf.reshape(..., [256, 256, 3]) + 1.) * 127.5), tf.uint8))

示例:

import numpy as np
import tensorflow as tf

data = tf.eye(256, batch_shape=[1])
bgr = tf.stack([data, data, data], axis = 3)
png = tf.image.encode_png(tf.cast((tf.reshape(bgr, [256, 256, 3]) + 1.) * 127.5, tf.uint8))

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    png_data_ = sess.run(png)
    open("d:/temp.png", 'wb').write(png_data_)