Python tensorflow.python.ops.image_ops.encode_jpeg() Examples

The following are 8 code examples of tensorflow.python.ops.image_ops.encode_jpeg(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tensorflow.python.ops.image_ops , or try the search function .
Example #1
Source File: image_ops_test.py    From deep_image_model with Apache License 2.0 6 votes vote down vote up
def testSynthetic(self):
    with self.test_session(use_gpu=True) as sess:
      # Encode it, then decode it, then encode it
      image0 = constant_op.constant(_SimpleColorRamp())
      jpeg0 = image_ops.encode_jpeg(image0)
      image1 = image_ops.decode_jpeg(jpeg0)
      image2 = image_ops.decode_jpeg(image_ops.encode_jpeg(image1))
      jpeg0, image0, image1, image2 = sess.run([jpeg0, image0, image1, image2])

      # The decoded-encoded image should be similar to the input
      self.assertLess(self.averageError(image0, image1), 0.6)

      # We should be very close to a fixpoint
      self.assertLess(self.averageError(image1, image2), 0.02)

      # Smooth ramps compress well (input size is 153600)
      self.assertGreaterEqual(len(jpeg0), 5000)
      self.assertLessEqual(len(jpeg0), 6000) 
Example #2
Source File: test_utils.py    From lambda-packs with MIT License 5 votes vote down vote up
def _encoder(image, image_format):
  assert image_format in ['jpeg', 'png']
  if image_format == 'jpeg':
    tf_image = constant_op.constant(image, dtype=dtypes.uint8)
    return image_ops.encode_jpeg(tf_image)
  if image_format == 'png':
    tf_image = constant_op.constant(image, dtype=dtypes.uint8)
    return image_ops.encode_png(tf_image) 
Example #3
Source File: tfexample_decoder_test.py    From auto-alt-text-lambda-api with MIT License 5 votes vote down vote up
def _Encoder(self, image, image_format):
    assert image_format in ['jpeg', 'JPEG', 'png', 'PNG', 'raw', 'RAW']
    if image_format in ['jpeg', 'JPEG']:
      tf_image = constant_op.constant(image, dtype=dtypes.uint8)
      return image_ops.encode_jpeg(tf_image)
    if image_format in ['png', 'PNG']:
      tf_image = constant_op.constant(image, dtype=dtypes.uint8)
      return image_ops.encode_png(tf_image)
    if image_format in ['raw', 'RAW']:
      return constant_op.constant(image.tostring(), dtype=dtypes.string) 
Example #4
Source File: test_utils.py    From auto-alt-text-lambda-api with MIT License 5 votes vote down vote up
def _encoder(image, image_format):
  assert image_format in ['jpeg', 'png']
  if image_format == 'jpeg':
    tf_image = constant_op.constant(image, dtype=dtypes.uint8)
    return image_ops.encode_jpeg(tf_image)
  if image_format == 'png':
    tf_image = constant_op.constant(image, dtype=dtypes.uint8)
    return image_ops.encode_png(tf_image) 
Example #5
Source File: tfexample_decoder_test.py    From tf-slim with Apache License 2.0 5 votes vote down vote up
def _Encoder(self, image, image_format):
    assert image_format in ['jpeg', 'JPEG', 'png', 'PNG', 'raw', 'RAW']
    if image_format in ['jpeg', 'JPEG']:
      tf_image = tf.constant(image, dtype=tf.uint8)
      return image_ops.encode_jpeg(tf_image)
    if image_format in ['png', 'PNG']:
      tf_image = tf.constant(image, dtype=tf.uint8)
      return image_ops.encode_png(tf_image)
    if image_format in ['raw', 'RAW']:
      # If machine is big endian, change the byte ordering in case of dtype
      # float32 so that it should be interpreted correctly.
      if image.dtype == np.float32 and sys.byteorder == 'big':
        image = image.astype('<f4')
      return tf.constant(image.tostring(), dtype=tf.string) 
Example #6
Source File: image_ops_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def testExisting(self):
    # Read a real jpeg and verify shape
    path = ('tensorflow/core/lib/jpeg/testdata/'
            'jpeg_merge_test1.jpg')
    with self.test_session(use_gpu=True) as sess:
      jpeg0 = io_ops.read_file(path)
      image0 = image_ops.decode_jpeg(jpeg0)
      image1 = image_ops.decode_jpeg(image_ops.encode_jpeg(image0))
      jpeg0, image0, image1 = sess.run([jpeg0, image0, image1])
      self.assertEqual(len(jpeg0), 3771)
      self.assertEqual(image0.shape, (256, 128, 3))
      self.assertLess(self.averageError(image0, image1), 0.8) 
Example #7
Source File: tfexample_decoder_test.py    From keras-lambda with MIT License 5 votes vote down vote up
def _Encoder(self, image, image_format):
    assert image_format in ['jpeg', 'JPEG', 'png', 'PNG', 'raw', 'RAW']
    if image_format in ['jpeg', 'JPEG']:
      tf_image = constant_op.constant(image, dtype=dtypes.uint8)
      return image_ops.encode_jpeg(tf_image)
    if image_format in ['png', 'PNG']:
      tf_image = constant_op.constant(image, dtype=dtypes.uint8)
      return image_ops.encode_png(tf_image)
    if image_format in ['raw', 'RAW']:
      return constant_op.constant(image.tostring(), dtype=dtypes.string) 
Example #8
Source File: test_utils.py    From keras-lambda with MIT License 5 votes vote down vote up
def _encoder(image, image_format):
  assert image_format in ['jpeg', 'png']
  if image_format == 'jpeg':
    tf_image = constant_op.constant(image, dtype=dtypes.uint8)
    return image_ops.encode_jpeg(tf_image)
  if image_format == 'png':
    tf_image = constant_op.constant(image, dtype=dtypes.uint8)
    return image_ops.encode_png(tf_image)