下面讲解tensorflow如何读取jpg格式的图片,png格式的图片是一样的。有两种情况:
第一种就是把图片看做是一个图片直接读进来,获取图片的原始数据,再进行解码,主要用到的函数就是tf.gfile.FastGFile,tf.image.decode_jpeg
例如:
import tensorflow as tf;
image_raw_data = tf.gfile.FastGFile('/home/penglu/Desktop/11.jpg').read()
image = tf.image.decode_jpeg(image_raw_data) #图片解码
print image.eval(session=tf.Session())
输出:
[[[ 11 63 110]
[ 14 66 113]
[ 17 69 116]
...,
第二种方式就是把图片看看成一个文件,用队列的方式读取
例如:
import tensorflow as tf;
path = '/home/penglu/Desktop/11.jpg'
file_queue = tf.train.string_input_producer([path]) #创建输入队列
image_reader = tf.WholeFileReader()
_, image = image_reader.read(file_queue)
image = tf.image.decode_jpeg(image)
with tf.Session() as sess:
coor