修改书上的代码,添加少量注释。
环境:python 3
#导入tensorflow
import tensorflow as tf
#新建一个Session
sess = tf.Session()
#tf.train.string_input_producer产生一个文件名队列
#shuffle属性在读取多个文件事需要注意
filename_queue = tf.train.string_input_producer(tf.train.match_filenames_once("blue_jay.jpg"),shuffle=False)
reader = tf.WholeFileReader()
key,value = reader.read(filename_queue)
image = tf.image.decode_jpeg(value)
#very important
#书上的初始化方式在0.12版本以后就换成global_variables_initializer()
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess,coord=coord)
#第一张图片
flipImageUpDown = tf.image.encode_jpeg(tf.image.flip_up_down(image))
file = open("flippedUpDown.jpg","wb+")
file.write(flipImageUpDown.eval(session=sess))
file.close()
#第二张图片
flipImageLeftRight = tf.image.encode_jpeg(tf.image.flip_left_right(image))
file = open("flippedLeftRight.jpg","wb+")
file.write(flipImageLeftRight.eval(session=sess))
file.close()
example = sess.run(flipImageLeftRight)
print(example)
coord.request_stop()
coord.join(threads)
sess.close()