tensorflow实现对图像的编码

来源:互联网 发布:西部数码 域名管理 编辑:程序博客网 时间:2024/06/06 22:38

tensorflow里面含有编码函数,可以编码成为jpg或者png格式的图片。

例如:

import matplotlib.pyplot as plt;import tensorflow as tf;image_raw_data_jpg = tf.gfile.FastGFile('11.jpg', 'r').read()with tf.Session() as sess:img_data_jpg = tf.image.decode_jpeg(image_raw_data_jpg) #解码img_data_jpg = tf.image.convert_image_dtype(img_data_jpg, dtype=tf.uint8)encode_image_jpg = tf.image.encode_jpeg(img_data_jpg) #jpg编码encode_image_png = tf.image.encode_png(img_data_jpg)#png编码with tf.gfile.GFile('output.jpg', 'wb') as f:f.write(encode_image_jpg.eval())with tf.gfile.GFile('output.png', 'wb') as f:f.write(encode_image_png.eval())
结果: