第2种方法:tensorflow进行操作,保存为tensor:
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 9 12:48:17 2017
@author: Administrator
"""
import tensorflow as tf
import numpy as np
import os
from PIL import Image
cur_dir = os.getcwd()
print("For Tensorflow to load images")
print("current directory:",cur_dir)
def modify_image(image):
resized = tf.image.resize_images(image, [80, 80], 1)
resized.set_shape([80,80,3])
flipped_images = tf.image.flip_up_down(resized)
return flipped_images
def read_image(filename_queue):
reader = tf.WholeFileReader()
key,value = reader.read(filename_queue)
image = tf.image.decode_png(value)
return key,image
def inputs():
# 手写数字--灰度图像
filenames = ['./Test/test/1.png', './Test/test/2.png' ]
# 加载所有的图像
FullImageList = [];
filenames = os.listdir('./Test/test/')
for fn in filenames:
FullImage = os.path.join( cur_dir, 'Test/test', fn )
FullImageList.append(FullImage)
filename_queue = tf.train.string_input_producer(filenames)
filename,read_input = read_image(filename_queue)
reshaped_image = modify_image(read_input)
return filename,reshaped_image
with tf.Graph().as_default():
image = inputs()
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
tf.train.start_queue_runners(sess=sess)
for i in range(3):
filename,img = sess.run(image)
print (filename)
# 手写数字--灰度图像
img = Image.fromarray(img[:,:,0], "L")
img.save(os.path.join(cur_dir+'/Test/',"ysw"+str(i)+".bmp"))
参考连接:https://stackoverflow.com/questions/34783030/saving-image-files-in-tensorflow