tf.gfile.FastGFile函数

烈酒焚心 提交于 2020-02-27 14:46:28

原文链接:https://blog.csdn.net/william_hehe/article/details/78821715
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())  
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!