预处理图像
文件名: cat.jpg
读取、打印图片
import matplotlib.pyplot as plt import tensorflow as tf import numpy as np image_raw_data = tf.gfile.FastGFile("./cat.jpg",'rb').read() with tf.Session() as sess: img_data = tf.image.decode_jpeg(image_raw_data) # 输出解码之后的三维矩阵。 #print(img_data.eval()) #print(img_data.get_shape()) img_data.set_shape([1797, 2673, 3]) print(img_data.get_shape()) with tf.Session() as sess: plt.imshow(img_data.eval()) #print(img_data.get_shape().as_list()) plt.show()
调整图片大小
with tf.Session() as sess: # 如果直接以0-255范围的整数数据输入resize_images,那么输出将是0-255之间的实数,不利于后续处理。 #如果直接以0-1之间的实数数据输入resize_images,那么输出将是0-1之间的实数。 #建议在调整图片大小前,先将图片转为0-1范围的实数。 #image_float=tf.cast(img_data, tf.float32)/255 image_float = tf.image.convert_image_dtype(img_data, tf.float32) #print(image_float.eval()) resized = tf.image.resize_images(image_float, [300, 300], method=0) #print(resized.eval()) plt.imshow(resized.eval()) plt.show()