TensorFlow提供了几类图像处理函数,下面介绍图像的编码与解码,图像尺寸调整。
编码与解码
图像解码与编码:一张RGB三通道的彩色图像可以看成一个三维矩阵,矩阵中的不位置上的数字代表图像的像素值。然后图像在存储时并不是直接记录这些矩阵中的数字,而是经过了压缩编码。所以将一张图像还原成一个三维矩阵的过程就是解码的过程,反之就是编码了。其实如果大家熟悉opencv的话,imread和imwrite就是一个解码和编码的过程。
TensorFlow提供了常用图片格式的解码和编码操作,下面用一个jpg的图像演示:
import matplotlib.pyplot as plt
import tensorflow as tf
image_raw_data = tf.gfile.FastGFile('.//image//1.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() #img_data = tf.image.convert_image_dtype(img_data,dtype = tf.float32) encoded_image = tf.image.encode_jpeg(img_data) with tf.gfile.GFile(".//image//3.jpg","wb") as f: f.write(encoded_image.eval())
其中:
decode_jpeg函数为jpeg(jpg)图片解码的过程,对应的encode_jpeg