python - 如何将张量保存为图像?
问题描述
我想将张量保存为特定文件夹中的图像。我正在使用 tf.write() 但我不知道它将图像保存在哪里。请告诉我如何将其保存为 jpeg 图像
path = "C:/Users/waqas/Desktop/0"
dirs = os.listdir( path )
for file in dirs:
img = flip_images(file)
img2 = tf.image.convert_image_dtype(img, tf.uint8,saturate=True)
enc = tf.image.encode_png(img2)
file_name = tf.constant(file)
files = tf.write_file(file_name, enc)
解决方案
使用以下代码:
import tensorflow as tf
import os
from PIL import Image
path = "D:\\data\\"
new_path = "D:\\new_folder\\"
filenames = os.listdir(path)
filenames = [path+images for images in filenames]
no_of_files = len(filenames)
filename_queue = tf.train.string_input_producer(filenames, num_epochs=no_of_files)
reader = tf.WholeFileReader()
key,value = reader.read(filename_queue)
image = tf.image.decode_jpeg(value)
reshaped_image = tf.image.flip_left_right(image)
with tf.Session() as sess:
init = tf.group(tf.global_variables_initializer(),tf.local_variables_initializer())
sess.run(init)
tf.train.start_queue_runners()
for i in range(no_of_files):
img = sess.run(reshaped_image)
img = Image.fromarray(img)
img.save(os.path.join(new_path+str(i)+".jpeg"))
推荐阅读
- scala - 在 spark DataFrame 中转换列而不创建空值
- sharepoint - 在 SharePoint On Premise 中上传 Web 部件时出现应用程序清单错误
- c - 在我看来是 C 中的缩进问题吗?我也认为我的数学运算符有点不对劲
- python - 是否可以在python中同时运行两个无限while循环
- sql - 有没有办法可以增强 Oracle 12C 中的以下查询
- cloud - 如何访问存储在 COS (IBM Cloud) 中的对象并将其显示到使用 Django 构建并部署在 Cloud Foundry 中的网页上?
- ansible - 当我运行安装命令时,Ansible Galaxy yaml 文件中的集合被忽略
- pandas - 熊猫评估分配方法?
- python - 玩家在 pygame 中不动;没有错误信息
- python - 熊猫分组输出