下面代码演示了如何对jpeg格式图像进行编码/解码,
代码如下:
import matplotlib.pyplot as plt
import tensorflow as tf
# 读取图像的原始数据
image_raw_data = tf.gfile.FastGFile("D:/path/to/picture/picture.jpg", 'rb').read()
with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw_data)
print(img_data.eval())
plt.imshow(img_data.eval())
plt.show()
encode_image = tf.image.encode_jpeg(img_data)
with tf.gfile.GFile("D:/path/to/picture/output.jpg", "wb") as f:
f.write(encode_image.eval())