import tensorflow as tf
import numpy as np
from matplotlib import pyplot as plt
import matplotlib as mpl
from PIL import Image,ImageOps
import os
import scipy.misc
file_names = '/Users/macbookair/Desktop/deadpool.jpg'
#导入图片
def get_picture1():
def read_one_image(filenames):
filename_queue = tf.train.string_input_producer([filenames])#将文件按照队列的方式导入,也必须按照队列的方式导入
image_reader = tf.WholeFileReader()#读入文件
_, image_file = image_reader.read(filename_queue)#从队列中抓取到image的文件
image = tf.image.decode_jpeg(image_file, channels=3)#解码方式,tf中支持JPG,PNG格式的图片,RGB,RGBA颜色空间
image = tf.cast(image, tf.float32) / 256.0 # cast to float to make conv2d work
return image
def get_real_images(image):
with tf.Session() as sess:
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
images = sess.run(image)
coord.request_stop()
coord.join(threads)
return images
#事实上read进来的就已经是我们想要的图片了,之后的threads等等操作只是为了让队列进入时不会出现问题
def get_picture2():
image = Image.open(file_names)#可以一步就读入我们要的照片
image = np.array(image)#必须进行array的操作,才能把导入的照片变成列表形式
#图片的操作
#有压缩等等很多操作,直接去官网搜,只记录一个变形状的
def get_resized_image(img_path, height, width, save=True):
image = Image.open(img_path)
# it's because PIL is column major so you have to change place of width and height
# this is stupid, i know
image = ImageOps.fit(image, (width, height), Image.ANTIALIAS)
if save:
image_dirs = img_path.split('/')
image_dirs[-1] = 'resized_' + image_dirs[-1]
out_path = '/'.join(image_dirs)#这三步的操作就是在原来的保存路径之上加上了resized,再次保存
if not os.path.exists(out_path):
image.save(out_path)#顺手把调整后的图片保存了
image = np.asarray(image, np.float32)
return np.expand_dims(image, 0)#加一维,只是为了conv2d的操作
#图片的可视化
def look_picture1(images):
fig = plt.figure()
plt.imshow(images)
plt.show()#直接跑完程序就显示了
#也可以从tensorboard中查看
def look_picture2(images):
img_summary = tf.summary.image('name',tf.expand_dims(images,0))
#这里的images一般为用tf的操作得到的预处理后的图片
#存储图片
def save_image(path, image):
image = np.clip(image, 0, 255).astype('uint8')#确保image的值在0-255之间,也可以用tf.minimum(image,255)来确保其中的值小于255
scipy.misc.imsave(path, image)
import numpy as np
from matplotlib import pyplot as plt
import matplotlib as mpl
from PIL import Image,ImageOps
import os
import scipy.misc
file_names = '/Users/macbookair/Desktop/deadpool.jpg'
#导入图片
def get_picture1():
def read_one_image(filenames):
filename_queue = tf.train.string_input_producer([filenames])#将文件按照队列的方式导入,也必须按照队列的方式导入
image_reader = tf.WholeFileReader()#读入文件
_, image_file = image_reader.read(filename_queue)#从队列中抓取到image的文件
image = tf.image.decode_jpeg(image_file, channels=3)#解码方式,tf中支持JPG,PNG格式的图片,RGB,RGBA颜色空间
image = tf.cast(image, tf.float32) / 256.0 # cast to float to make conv2d work
return image
def get_real_images(image):
with tf.Session() as sess:
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
images = sess.run(image)
coord.request_stop()
coord.join(threads)
return images
#事实上read进来的就已经是我们想要的图片了,之后的threads等等操作只是为了让队列进入时不会出现问题
def get_picture2():
image = Image.open(file_names)#可以一步就读入我们要的照片
image = np.array(image)#必须进行array的操作,才能把导入的照片变成列表形式
#图片的操作
#有压缩等等很多操作,直接去官网搜,只记录一个变形状的
def get_resized_image(img_path, height, width, save=True):
image = Image.open(img_path)
# it's because PIL is column major so you have to change place of width and height
# this is stupid, i know
image = ImageOps.fit(image, (width, height), Image.ANTIALIAS)
if save:
image_dirs = img_path.split('/')
image_dirs[-1] = 'resized_' + image_dirs[-1]
out_path = '/'.join(image_dirs)#这三步的操作就是在原来的保存路径之上加上了resized,再次保存
if not os.path.exists(out_path):
image.save(out_path)#顺手把调整后的图片保存了
image = np.asarray(image, np.float32)
return np.expand_dims(image, 0)#加一维,只是为了conv2d的操作
#图片的可视化
def look_picture1(images):
fig = plt.figure()
plt.imshow(images)
plt.show()#直接跑完程序就显示了
#也可以从tensorboard中查看
def look_picture2(images):
img_summary = tf.summary.image('name',tf.expand_dims(images,0))
#这里的images一般为用tf的操作得到的预处理后的图片
#存储图片
def save_image(path, image):
image = np.clip(image, 0, 255).astype('uint8')#确保image的值在0-255之间,也可以用tf.minimum(image,255)来确保其中的值小于255
scipy.misc.imsave(path, image)
本文介绍如何使用TensorFlow和相关库加载、预处理图片,并展示如何调整图片大小、可视化及保存处理后的图片。涵盖图片文件读取、队列管理、尺寸调整等关键技术。

1152

被折叠的 条评论
为什么被折叠?



