Exemple #1
0
def write_png(filename, image):
    """
    Saves an image to a PNG file. Taken from Balle's implementation
    """
    image = quantize_image(image)
    string = tf.image.encode_png(image)
    return tf.write_file(filename, string)
Exemple #2
0
    def decode(self, ids, strip_extraneous=False):
        """Transform a sequence of int ids into an image file.

    Args:
      ids: list of integers to be converted.
      strip_extraneous: unused

    Returns:
      Path to the temporary file where the image was saved.

    Raises:
      ValueError: if the ids are not of the appropriate size.
    """
        del strip_extraneous
        _, tmp_file_path = tempfile.mkstemp('_decode.png')
        if self._height is None or self._width is None:
            size = int(math.sqrt(len(ids) / self._channels))
            length = size * size * self._channels
        else:
            size = None
            length = self._height * self._width * self._channels
        if len(ids) != length:
            raise ValueError(
                'Length of ids (%d) must be height (%d) x width (%d) x '
                'channels (%d); %d != %d.\n Ids: %s' % (
                    len(ids),
                    self._height,
                    self._width,
                    self._channels,
                    len(ids),
                    length,
                    ' '.join([str(i) for i in ids]),
                ))
        with tf.Graph().as_default():
            raw = tf.constant(ids, dtype=tf.uint8)
            if size is None:
                img = tf.reshape(raw,
                                 [self._height, self._width, self._channels])
            else:
                img = tf.reshape(raw, [size, size, self._channels])
            png = tf.image.encode_png(img)
            op = tf.write_file(tmp_file_path, png)
            with tf.Session() as sess:
                sess.run(op)
        return tmp_file_path
def write_png(filename, image):
  """Saves an image to a PNG file."""
  image = quantize_image(image)
  string = tf.image.encode_png(image)
  return tf.write_file(filename, string)