Reputation: 5844
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.
Upvotes: 3
Views: 8899
Reputation: 5162
Alternatively, you can evaulate the image and use opencv or PIL to save it.
import cv2 # opencv
from scipy.misc import imsave
... network generates an image ...
out = network(input)
img = tf.image.encode_jpeg(out)
img_to_save = img.eval(feed_dict)
# cv2.imwrite("myimage", img_to_save)
imsave("myimage.jpeg", img_to_save)
your image may or may not need a feed_dict to be evaluated.
Upvotes: 0
Reputation: 48330
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
Upvotes: 5