Tensorflow学习笔记之存取图像文件

来源:互联网 发布:linux 文件下载 编辑:程序博客网 时间:2024/06/02 21:37

读图的时候是用 tensorflow 的函数,存图像用的 scipy.misc 的 imsave 函数。

因为在使用 tensorflow 的队列时对存图像大小的限制有疑问所以进行测试。

import tensorflow as tfimport numpy as npimport scipy.misc#读取图像可任意大小filenames = ['img1.png','img2.png'] filename_queue = tf.train.string_input_producer(filenames)reader = tf.WholeFileReader()key,value = reader.read(filename_queue)images = tf.image.decode_png(value)#设置图像大小newsize = tf.convert_to_tensor([256,256])resized = tf.image.resize_images(images, newsize) print(resized)  #输出是Tensor("Squeeze:0", shape=(256, 256, ?), dtype=float32)resized.set_shape([256,256,3])print(resized)#输出是Tensor("Squeeze:0", shape=(256, 256, 3), dtype=float32)flipped_images = tf.image.flip_up_down(resized)print(flipped_images)  #输出是Tensor("ReverseV2:0", shape=(256, 256, 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('reimg1.png', reimg1)    scipy.misc.imsave('reimg2.png', reimg2)    coord.request_stop()    coord.join(threads)

读取图像有128、256、512、1024大小的,存储时也试过这些像素,可见 scipy.misc.imsave 方法可以很容易的将 numpy 数据存成图像格式,且基本没有大小限制。

还有一种方法是用 Pillow 库的 Image 类存取图像:

from PIL import Image# 读取图像可任意大小im = Image.open("img1.png")# 设定图像大小im.thumbnail([256,256])# 存储图像数据im.save("img1-256x256.png")
这个方法不灵活,因为存储数据必须是Image类的对象。

阅读全文
0 0
原创粉丝点击