本文整理汇总了Python中tensorflow.python.ops.image_ops.encode_png函数的典型用法代码示例。如果您正苦于以下问题:Python encode_png函数的具体用法?Python encode_png怎么用?Python encode_png使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了encode_png函数的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _make_sprite_image
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)
示例2: _encoder
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)
示例3: _Encoder
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)
示例4: testSyntheticTwoChannelUint16
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)
示例5: testSynthetic
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)
示例6: testExisting
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())