码迷,mamicode.com
首页 > 其他好文 > 详细

tensorflow图像处理函数(1)

时间:2018-03-07 21:47:02      阅读:477      评论:0      收藏:0      [点我收藏+]

标签:结果   部分   tensor   生成   一个   输出   tput   解码   float   

1、tensorflow中对jpeg格式图像的编码/解码函数:

import matplotlib.pyplot as plt
import tensorflow as tf
image_raw_data=tf.gfile.FastGFile(/Users/jk/Downloads/timg.jpeg,rb).read()
with tf.Session() as sess:
    img_data=tf.image.decode_jpeg(image_raw_data)    #通过tf.img.decode_jpeg函数对jpeg格式的图像进行解码,解码后的结果为一个张量
    print(img_data.eval())      #输出解码后的三维矩阵
    plt.imshow(img_data.eval())
    plt.show()
    img_data=tf.image.convert_image_dtype(img_data,dtype=tf.uint8)    
    encode_image=tf.image.encode_jpeg(img_data)     #将图像的三维矩阵重新按照jpeg格式编码存入文件,打开该图像可以得到和原始图像一样的图像
    with tf.gfile.GFile(/Users/jk/Downloads/output,wb) as f:   #将文件写入目标路径,生成图像文件
        f.write(encode_image.eval())

 2、图像大小调整(和上面的类似,仅多了图像大小调整的部分,下面的例子将类似):

import matplotlib.pyplot as plt
import tensorflow as tf
image_raw_data=tf.gfile.FastGFile(/Users/jk/Downloads/timg.jpeg,rb).read()
with tf.Session() as sess:
    img_data=tf.image.decode_jpeg(image_raw_data)
    print(img_data.eval())
    plt.imshow(img_data.eval())
    plt.show()
    img_data=tf.image.convert_image_dtype(img_data,dtype=tf.uint8)
    resized=tf.image.resize_images(img_data,size=[300,300],method=1)    #将图像大小转为[300,300],图像深度在没有明确设置前会是?,
    print(resized.get_shape())
    resized=tf.image.convert_image_dtype(resized,dtype=tf.uint8)     #数据预处理时候会把dtype转为tf.float32,因此需要手动转回tf.uint8
    encode_image=tf.image.encode_jpeg(resized)
    with tf.gfile.GFile(/Users/jk/Downloads/output,wb) as f:    #返回调整大小后的图像
        f.write(encode_image.eval())      

 

tensorflow图像处理函数(1)

标签:结果   部分   tensor   生成   一个   输出   tput   解码   float   

原文地址:https://www.cnblogs.com/xiaochouk/p/8525006.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!