参考:https://blog.csdn.net/u014802590/article/details/68495238

参考:https://www.2cto.com/kf/201709/680575.html

1、读取图片文件并写为TFRecords文件

import tensorflow as tf
from PIL import Image
import os
file_path='C:/Users/1/Desktop/123/'
file_name=os.listdir(file_path)
writer=tf.python_io.TFRecordWriter('C:/Users/1/Desktop/fuck.tfrecords')
for name in file_name:
img_path=file_path+name
img=Image.open(img_path)
img=img.resize((10,10))
img_raw=img.tobytes()
print(index)
example=tf.train.Example(features=tf.train.Features(feature={'label':tf.train.Feature(int64_list=tf.train.Int64List(value=[index])),'img_raw':tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))}))
imgg=example.SerializeToString()
writer.write(imgg)
writer.close()

2、读取图片文件对应的tfrecords文件:

import scipy.misc as misc
import matplotlib.pyplot as plt
filename=tf.train.match_filenames_once('C:/Users/1/Desktop/fuck.*frecords')
files=tf.train.string_input_producer(filename,shuffle=False)
reader=tf.TFRecordReader()
_,seri=reader.read(files)
feature=tf.parse_single_example(seri,features={'label':tf.FixedLenFeature([],tf.int64),'img_raw':tf.FixedLenFeature([],tf.string)})
img=feature['img_raw']
img=tf.decode_raw(img,tf.uint8)
img=tf.reshape(img,[256,256,3])
img=tf.cast(img,tf.float32)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
coord=tf.train.Coordinator()
threads=tf.train.start_queue_runners(sess=sess,coord=coord)
img=sess.run(img)
plt.imshow(img)
plt.show() #plot.show()展示的是黑白图
misc.imsave('C:/Users/1/Desktop/1111.jpg', img) #使用scipy.misc.imsave来保存np.array数组格式的图片,保存好的图片为彩色图片
coord.request_stop()
coord.join(threads)

TFrecords读、写图片文件的更多相关文章

  1. C++文件处理(一):读/写txt文件

    C++文件处理与C语言不同,C++文件处理使用的是:流(stream) C++头文件fstream定义了三个类型来支持文件IO

  2. android 读,写图片sd网卡资源

    <pre name="code" class="html"><!--<span style="font-family: Ari ...

  3. Python小实验——读&写Excel文件内容

    安装xlrd模块和xlwt模块 读取Excel文件了内容需要额外的模块-- \(xlrd\),在官网上可以找到下载:https://pypi.python.org/pypi/xlrd#download ...

  4. 读/写xlsx文件

    安装 pip install openpyxl 1.创建Excel电子表格 建立新文档需要调用Workbook对象的save方法,一个Workbook对象代表一个Excel工作簿,该方法的参数是保存的 ...

  5. 读/写docx文件

    安装 pip install python-docx 1.建立新Word文档 建立新文档需要调用Document对象的save方法,一个Document对象代表一个Word文档,该方法的参数是保存的文 ...

  6. 【学习总结】GirlsInAI ML-diary day-15-读/写txt文件

    [学习总结]GirlsInAI ML-diary 总 原博github链接-day15 认识读/写txt文件 路径: 绝对路径:文件在电脑中的位置 相对路径:下面会用到 1-准备 新建一个 pytho ...

  7. python读、写、修改、追写excel文件

    三个工具包 python操作excel的三个工具包如下 xlrd: 对excel进行读相关操作 xlwt: 对excel进行写相关操作 xlutils: 对excel读写操作的整合 注意,只能操作.x ...

  8. PLSQL_PLSQL读和写XML文件方式(案例)

    2012-05-01 Created By BaoXinjian

  9. PLSQL_PLSQL读和写CSV文件方式(案例)

    2012-01-06 Created By BaoXinjin

随机推荐

  1. 关于.NET Core的一些问题和疑惑

    1 为什么会出现.NET Core这个东西?即它为了解决什么问题. .NET Core是NET的ECMA标准的一种新的实现.目前.NET已有Framework,Mono,Unity等实现. 原先所有的 ...

  2. React - 可控组件和非可控组件的选择

    原则 受控组件(用户输入 ---> state 更新 ---> 组件更新)的消耗明显比非受控组件大的多,但非受控组件只能在需求非常简单的情况下的使用. 特性 uncontrolled 受控 ...

  3. Week1 - 169.Majority Element

    这周刚开始讲了一点Divide-and-Conquer的算法,于是这周的作业就选择在LeetCode上找分治法相关的题目来做. 169.Majority Element Given an array ...

  4. Vue Router 路由守卫:完整的导航解析流程

    完整的导航解析流程 1 导航被触发. 2 在失活的组件里调用离开守卫. 3 调用全局的 beforeEach 守卫. 4 在重用的组件里调用 beforeRouteUpdate 守卫 (2.2+). ...

  5. js中ajax请求返回的数据处理成数组后,局部变量赋值给全局变量后,为空

    第二步是想把ss的值扔给res_r,两个数组直接相等即可,可谁想到,取出来的值是空. 如图取出来的值是空. 我一脸懵逼,调试了些许时间,最后把ss遍历一下,在重新push进res_r 再来看效果,已经 ...

  6. 003/node.js--代理服务(解决跨域问题)

    业务描述: 1.web前端发送http请求 2.web后端为https协议 如何保证web前端发送http请求到web后端(跨域问题:域名不一致即跨域), 因此用node.js写了个代理服务,转发前端 ...

  7. stl(set或map)

    https://nanti.jisuanke.com/t/41384 There are nnn points in an array with index from 111 to nnn, and ...

  8. H-Updating a Dictionary (模拟)

    In this problem, a dictionary is collection of key-value pairs, where keys are lower-case letters, a ...

  9. wxpython中单选框的两种创建方式源码展示

    #coding=utf-8 import wx class MyFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self,None,-1, ...

  10. [SheetJS] js-xlsx模块学习指南

    简介 SheetJS是前端操作Excel以及类似的二维表的最佳选择之一,而js-xlsx是它的社区版本. js-xlsx将注意力集中到了数据转换和导出上,所以它支持相当多种类的数据解析和导出.不仅仅局 ...