Saving image files in Tensorflow
<p>I'm just starting with Tensorflow and I have a newbie question.</p>

<p>I know that Tensorflow is all about neural nets but I'm starting with just the mechanics of it. I'm trying to get it to load, resize, flip, and save two images. Should be a simple operation, right, and it gets me started with the basics.</p>

<p>Here's my code so far:</p>

<pre><code>import tensorflow as tf
import numpy as np

print("resizing images")

filenames = ['img1.png', 'img2.png' ]
filename_queue = tf.train.string_input_producer(filenames, num_epochs=1)

reader = tf.WholeFileReader()
key,value = reader.read(filename_queue)
images = tf.image.decode_png(value)

resized = tf.image.resize_images(images, 180,180, 1)
resized.set_shape([180,180,3])

flipped_images = tf.image.flip_up_down(resized)

resized_encoded = tf.image.encode_jpeg(flipped_images,name="save_me")

init = tf.initialize_all_variables()
sess = tf.Session()

with sess.as_default():
  tf.train.start_queue_runners()
  sess.run(init)

  f = open("/tmp/foo1.jpeg", "wb+")
  f.write(resized_encoded.eval())
  f.close()

  f = open("/tmp/foo2.jpeg", "wb+")
  f.write(resized_encoded.eval())
  f.close()
</code></pre>

<p>It works fine, resizing the two images and saving them. But it always ends with an error:</p>

<pre><code>W tensorflow/core/common_runtime/executor.cc:1076] 0x7f97240e7a40
 Compute status: Out of range: Reached limit of 1
</code></pre>

<p>I'm obviously doing something wrong. If I take off the num_epochs=1, then it ends with no error.</p>

<p>I have a few questions:</p>

<p>How do I do this correctly?</p>

<p>Also, if I want to preserve the original file names all the way from the filename_queue through to the end so I can save them with the original names, how do I do that? And how do I know how many files I need to save? Let's say I'm making the list of file names by reading a directory. I tried many different things but I could never find out how I know when I reach the end.</p>

<p>It seems strange to me that I'm calling resized_encoded.eval() twice.</p>

<p>Thank you and I'm sure this is a very basic question but I'm not understanding how this works.</p>

<p>Edit: I created an even simpler demonstration of the behavior:</p>

<pre><code>import tensorflow as tf
import numpy as np

filenames = ['file1.png', 'file2.png' ]

filename_queue = tf.train.string_input_producer(filenames, 
                        num_epochs=1, name="my_file_q")

reader = tf.WholeFileReader()
key,value = reader.read(filename_queue)
init = tf.initialize_all_variables()

sess = tf.Session()

with sess.as_default():
  print("session started")

  sess.run(init)

  coord = tf.train.Coordinator()
  threads = tf.train.start_queue_runners(coord=coord)

  for i in range (2):
    print(key.eval())

  coord.request_stop()
  coord.join(threads)
</code></pre>

<p>This gives the same warning. I can't understand why.</p>