tf.gfile.FastGFile

本文介绍如何利用TensorFlow的tf.gfile.FastGFile函数读取图像文件,并通过tf.image.decode_jpeg和tf.image.decode_png进行图像解码。文章详细展示了从文件系统读取jpg和png格式图像的具体代码,以及如何将读取到的数据转换为适合深度学习模型使用的数据类型。

tf.gfile.FastGFile(path,decodestyle) 
函数功能:实现对图片的读取。 
函数参数:(1)path:图片所在路径 (2)decodestyle:图片的解码方式。(‘r’:UTF-8编码; ‘rb’:非UTF-8编码)


import matplotlib.pyplot as plt 
import tensorflow as tf 
 
#tf.gfileGFile()函数:读取图像  
image_jpg = tf.gfile.FastGFile('dog.jpg','rb').read()  
image_png = tf.gfile.FastGFile('lizard.png','rb').read()  
 
with tf.Session() as sess:  
 
    image_jpg = tf.image.decode_jpeg(image_jpg) #图像解码
    print(sess.run(image_jpg))#打印解码后的图像(即为一个三维矩阵[w,h,3])
    image_jpg = tf.image.convert_image_dtype(image_jpg,dtype=tf.uint8) #改变图像数据类型  
 
    image_png = tf.image.decode_png(image_png) 
    print(sess.run(image_jpg))
    image_png = tf.image.convert_image_dtype(image_png,dtype=tf.uint8)  
 
    plt.figure(1) #图像显示  
    plt.imshow(image_jpg.eval())  
    plt.figure(2)  
    plt.imshow(image_png.eval()) 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值