TensorFlow文件操作

来源:互联网 发布:skype mac 编辑:程序博客网 时间:2024/05/19 03:24

本文演示如何读取文件,并输出文件到另外的目录。

# 导入TensorFlowimport tensorflow as tffilename = ['A.png', 'B.png', 'C.png', 'D.png']epoch = 2# string_input_producer会产生一个文件名队列filename_queue = tf.train.string_input_producer(filename, shuffle=False, num_epochs=epoch)# reader从文件名队列中读数据。reader = tf.WholeFileReader()key, value = reader.read(filename_queue)# 新建一个Sessionwith tf.Session() as sess:    # tf.train.string_input_producer定义了一个epoch变量,要对它进行初始化    tf.local_variables_initializer().run()    # 使用start_queue_runners之后,才会开始填充队列    threads = tf.train.start_queue_runners(sess=sess)    i = 0    count = len(filename) * epoch    for i in range(count):        i += 1        # 获取图片数据并保存        image_data = sess.run(value)        with open('read/test_%d.jpg' % i, 'wb') as f:            f.write(image_data)


原创粉丝点击