title: Tensorflow: *Save* the output Conv layer as a png image
body: <p>I have managed to get the output of a convolution layer show in tensorboard using the code below. My big challenge and where I am hoping you can help me is that I would like to be able to get that output saved in a png file (for example).</p>
  
  <p>I have started doing it but cannot manage to get it to work 
  using PIL or tensorflow encode. I have attached the code between ###### BEGIN END #####</p>
  
  <p>Assuming that the output of my layer is a tensor of shape (64, 16, 16, 1) Is there a very simple way extract and save the first element of that tensor, i.e. (64, 64, 1), as a png/jpeg image using PIL/numpy or TF ? </p>
  
  <p>Thanks a lot in advance for your time and help.
  N.</p>
  
  <pre><code>def _view_layer(name, layer, w, h, d, training=True):
      L = tf.slice(layer, (0, 0, 0, 0), (1, -1, -1, -1), name='slice_first_input')
      L = tf.reshape(L, (w, h, d))
  
      L = tf.transpose(L, (2, 0, 1))
      # Bring into shape expected by image_summary
      L = tf.reshape(L, (-1, w, h, 1))
  
      ###### BEGIN save as image ### not working #################################
      if(training == False):
          print(L.get_shape())
          img_f = tf.image.resize_images(L, 64, 64, method=2)  # 224
          img_int = tf.cast(img_f, tf.uint8)
          print(img_int.get_shape())
          slice_im_2d = img_int[0, 2:66, 2:66, :] #&lt;=Not sure how to get (64, 64, 1)
          print(slice_im_2d.get_shape())
  
          # Trying with PIL
          # im = Image.fromarray(np.array(slice_im_2d).astype(np.uint8))
          # im.save( datetime.now() + ".png")
  
          # png_bin = tf.image.encode_png(slice_im_2d)
  
          # Trying with TensorFlow
          # with tf.Session().as_default():
          #     format_str = ('%s.png')
          #     fr = format_str % datetime.now()
          #     f = open(fr, "wb+")
          #     f.write(png_bin.eval())
          #     f.close()
      ###### END save as image ### not working #################################
  
      # show in Tensorboard summary
      tf.image_summary(name, V)
  </code></pre>