tensorflow 设置图片大小与翻转

简介

读图的时候是用 tensorflow 的函数,存图像用的save,发现报错,tensor对象不能进行直接保存操作,得用 scipy.misc 的 imsave 函数。

实例代码

import tensorflow as tf  
import numpy as np  
import scipy.misc  

#读取图像可任意大小  
filenames = ['E://Parking186//ImgReshape//1Thumbnail25.jpg','E://Parking186//ImgReshape//5Thumbnail39.jpg']   

filename_queue = tf.train.string_input_producer(filenames)  
reader = tf.WholeFileReader()  
key,value = reader.read(filename_queue)  
images = tf.image.decode_jpeg(value)#tf.image.decode_png(value)

#设置图像大小  
newsize = tf.convert_to_tensor([300,360])  
resized = tf.image.resize_images(images, newsize)   
print("resized:",resized)    
#输出是Tensor("Squeeze:0", shape=(300, 360, ?), dtype=float32)
resized.set_shape([300,360,3])  
print("resized.set_shape:",resized)  
#输出是Tensor("Squeeze:0", shape=(300, 360, 3), dtype=float32)

flipped_images = tf.image.flip_up_down(resized)  
print("flipped_images:",flipped_images)    
#输出是Tensor("ReverseV2:0", shape=(300, 360, 3), dtype=float32)

with tf.Session() as sess:  
    coord = tf.train.Coordinator()  
    threads = tf.train.start_queue_runners(coord=coord)  

    init = tf.global_variables_initializer()  
    sess.run(init)  

    #reimg1的类型是<class 'numpy.ndarray'>  
    reimg1 = flipped_images.eval()  
    reimg2 = flipped_images.eval()  

    scipy.misc.imsave('E:\\Parking186\\ImgReshape\\reimg1.jpg', reimg1)  
    scipy.misc.imsave('E:\\Parking186\\ImgReshape\\reimg2.jpg', reimg2)  

    coord.request_stop()  
    coord.join(threads)  
    print("done!")

效果演示

1、原图片一
这里写图片描述

设置图片大小:300×360
这里写图片描述

2、原图片二
这里写图片描述

设置图片大小:300×360
这里写图片描述

参考

[1]Tensorflow学习笔记之存取图像文件
http://blog.csdn.net/index20001/article/details/73843070
[2]学习TensorFlow,生成tensorflow输入输出的图像格式
http://blog.csdn.net/helei001/article/details/51354404

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

何以问天涯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值