def _make_sprite_image(thumbnails, thumbnail_dim): """Constructs a sprite image from thumbnails and returns the png bytes.""" if len(thumbnails) < 1: raise ValueError('The length of "thumbnails" must be >= 1') if isinstance(thumbnails, np.ndarray) and thumbnails.ndim != 4: raise ValueError('"thumbnails" should be of rank 4, ' 'but is of rank %d' % thumbnails.ndim) if isinstance(thumbnails, list): if not isinstance(thumbnails[0], np.ndarray) or thumbnails[0].ndim != 3: raise ValueError('Each element of "thumbnails" must be a 3D `ndarray`') thumbnails = np.array(thumbnails) with ops.Graph().as_default(): s = session.Session() resized_images = image_ops.resize_images(thumbnails, thumbnail_dim).eval( session=s) images_per_row = int(math.ceil(math.sqrt(len(thumbnails)))) thumb_height = thumbnail_dim[0] thumb_width = thumbnail_dim[1] master_height = images_per_row * thumb_height master_width = images_per_row * thumb_width num_channels = thumbnails.shape[3] master = np.zeros([master_height, master_width, num_channels]) for idx, image in enumerate(resized_images): left_idx = idx % images_per_row top_idx = int(math.floor(idx / images_per_row)) left_start = left_idx * thumb_width left_end = left_start + thumb_width top_start = top_idx * thumb_height top_end = top_start + thumb_height master[top_start:top_end, left_start:left_end, :] = image return image_ops.encode_png(master).eval(session=s)
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)
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)
def testSyntheticTwoChannelUint16(self): with self.test_session() as sess: # Strip the b channel from an rgb image to get a two-channel image. gray_alpha = _SimpleColorRamp()[:, :, 0:2] image0 = constant_op.constant(gray_alpha, dtype=dtypes.uint16) png0 = image_ops.encode_png(image0, compression=7) image1 = image_ops.decode_png(png0, dtype=dtypes.uint16) png0, image0, image1 = sess.run([png0, image0, image1]) self.assertEqual(2, image0.shape[-1]) self.assertAllEqual(image0, image1)
def testSynthetic(self): with self.test_session() as sess: # Encode it, then decode it image0 = constant_op.constant(_SimpleColorRamp()) png0 = image_ops.encode_png(image0, compression=7) image1 = image_ops.decode_png(png0) png0, image0, image1 = sess.run([png0, image0, image1]) # PNG is lossless self.assertAllEqual(image0, image1) # Smooth ramps compress well, but not too well self.assertGreaterEqual(len(png0), 400) self.assertLessEqual(len(png0), 750)
def testExisting(self): # Read some real PNGs, converting to different channel numbers prefix = 'tensorflow/core/lib/png/testdata/' inputs = (1, 'lena_gray.png'), (4, 'lena_rgba.png') for channels_in, filename in inputs: for channels in 0, 1, 3, 4: with self.test_session() as sess: png0 = io_ops.read_file(prefix + filename) image0 = image_ops.decode_png(png0, channels=channels) png0, image0 = sess.run([png0, image0]) self.assertEqual(image0.shape, (26, 51, channels or channels_in)) if channels == channels_in: image1 = image_ops.decode_png(image_ops.encode_png(image0)) self.assertAllEqual(image0, image1.eval())
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)