title: How do I write an encoded jpeg as bytes to Tensorflow tfrecord and then read it?
body: <p>I am trying to use tensorflows tfrecords format to store my datasets. </p>
  
  <p>I managed to read in jpeg images and decode them to raw format and write them to a tfrecord file. I can then later read them using tf.decode_raw.</p>
  
  <p>The problem is that this leads to huge file sizes because I am storing the images as raw. Now I have seen many tutorials and blogs saying I can store them in an encoded format and then when reading them just decode them. I can't find any example of this. I have been trying for a while but no matter what way I do it I'm getting formatting errors. </p>
  
  <p>TLDR
  Does anyone know how to write images to a tfrecord file as jpegs and not as raw.</p>
  
  <p>Thank You,
  David.</p>
  
  <p>My writing function.</p>
  
  <pre><code>def convert(image_paths, labels, out_path):
  
  num_images = len(image_paths)
  
  with tf.python_io.TFRecordWriter(out_path) as writer:
      for i, (path, label) in enumerate(zip(image_paths, labels)):
  
          print_progress(count=i, total=num_images-1)
          img = open(path, 'rb').read()
  
          data ={'image': wrap_bytes(img),
              'label': wrap_int64(label)}
  
          feature = tf.train.Features(feature=data)
          example = tf.train.Example(features=feature)
          serialized = example.SerializeToString()
          writer.write(serialized)
  </code></pre>
  
  <p>Convert dataset with this: </p>
  
  <pre><code>{convert(image_paths=image_paths_train,
      labels=cls_train,
      out_path=path_tfrecords_train)}
  </code></pre>
  
  <p>My Reading Function</p>
  
  <pre><code>def parse(serialized):
  
  features = \
      {
          'image': tf.FixedLenFeature([], tf.string),
          'label': tf.FixedLenFeature([], tf.int64)
      }
  parsed_example = tf.parse_single_example(serialized=serialized,
                                           features=features)
  
  # Get the image as raw bytes.
  image_raw = parsed_example['image']
  
  # Decode the raw bytes so it becomes a tensor with type.
  image = tf.image.decode_image(image_raw,channels=3)
  
  #image = tf.decode_raw(image_raw, tf.uint8)
  
  # The type is now uint8 but we need it to be float.
  image = tf.cast(image, tf.float32)
  
  # Get the label associated with the image.
  label = parsed_example['label']
  
  # The image and label are now correct TensorFlow types.
  return image, label
  </code></pre>