Skip to content

Instantly share code, notes, and snippets.

@psycharo-zz
Created February 3, 2017 12:57
Show Gist options
  • Save psycharo-zz/35d3f603e808de42ee17d01e2026ae85 to your computer and use it in GitHub Desktop.
Save psycharo-zz/35d3f603e808de42ee17d01e2026ae85 to your computer and use it in GitHub Desktop.
class ImageCoder(object):
"""Helper class for handling images in TensorFlow."""
def __init__(self, channels=3, config=None):
# Create a single TensorFlow Session for all image decoding calls.
self._sess = tf.Session(config=config)
# TensorFlow ops for JPEG decoding.
self._src_png = tf.placeholder(dtype=tf.string)
self._dst_raw = tf.image.decode_png(self._src_png, channels=channels)
self._src_raw = tf.placeholder(dtype=tf.uint8)
self._dst_png = tf.image.encode_png(self._src_raw)
def decode_png(self, src_png):
image = self._sess.run(self._dst_raw, {self._src_png: src_png})
return image
def encode_png(self, src_raw):
image_png = self._sess.run(self._dst_png, {self._src_raw: src_raw})
return image_png
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment