How do we use tf.image.encode_jpeg to write out an image in TensorFlow?
tensorflow
Solution
In tensorflow the function would look like:
import tensorflow as tf
def write_jpeg(data, filepath):
g = tf.Graph()
with g.as_default():
data_t = tf.placeholder(tf.uint8)
op = tf.image.encode_jpeg(data_t, format='rgb', quality=100)
init = tf.initialize_all_variables()
with tf.Session(graph=g) as sess:
sess.run(init)
data_np = sess.run(op, feed_dict={ data_t: data })
with open(filepath, 'w') as fd:
fd.write(data_np)
import numpy as np
R = np.zeros([128 * 128])
G = np.ones([128 * 128]) * 100
B = np.ones([128 * 128]) * 200
data = np.array(list(zip(R, G, B)), dtype=np.uint8).reshape(128, 128, 3)
assert data.shape == (128, 128, 3)
write_jpeg(data, "./test.jpeg")
the numpy part can be improved but it was for demonstration purposes only
Problem
Given a Tensor, I would like to use this function to get a 0D string and then write this out as "sample.jpg". This can be easily achieved by OpenCV or Python PIL, but I would like to keep everything within TF if possible.