使用 tensorflow 加载、解码、resize_bilinear()、然后编码和写入 jpeg 图像的工作示例?
Working example of loading, decoding, resize_bilinear(), then encoding, and writing a jpeg image using tensorflow?
除了下面这个小程序的垃圾输出,我什么也得不到。我只想
- 加载并解码 jpeg 图像
- 使用
tf.resize_bilinear
将其大小调整为 (224, 224)
- 将其重新编码为 jpeg 并保存到文件
将 tensorflow 导入为 tf
将 numpy 导入为 np
进口os
从 PIL 导入图片
cur_dir = os.getcwd()
print("resizing images")
print("current directory:",cur_dir)
def modify_image(image):
resize_shape = tf.stack([224, 224])
resize_shape_as_int = tf.cast(resize_shape, dtype=tf.int32)
#resized = tf.image.resize_bilinear(decoded_image_4d, resize_shape_as_int)
resized = tf.image.resize_images(image, resize_shape_as_int)
#image_3d = tf.squeeze(resized, squeeze_dims=[0])
image_3d = tf.image.convert_image_dtype(resized, tf.uint8, saturate=False)
return image_3d
def read_image(filename_queue):
reader = tf.WholeFileReader()
key,value = reader.read(filename_queue)
image = tf.image.decode_jpeg(value)
return key,image
def inputs(args):
filenames = args.input_files
filename_queue = tf.train.string_input_producer(filenames)
filename,read_input = read_image(filename_queue)
reshaped_image = modify_image(read_input)
img = tf.image.encode_jpeg(reshaped_image)
return filename,img
def parse_args():
a = argparse.ArgumentParser()
a.add_argument('input_files', nargs='+')
args = a.parse_args()
return args
def main():
args = parse_args()
with tf.Graph().as_default():
image = inputs(args)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
tf.train.start_queue_runners(sess=sess)
filename,img = sess.run(image)
with open(os.path.join(cur_dir, 'output.jpg'), 'wb') as fh:
fh.write(img)
if __name__ == '__main__':
main()
虽然我得到的只是垃圾数据,但输出的是这样的东西
tf.stack
接收 Tensor
个对象的列表而不是整数。
迟到的答案。这是一个解决方案,
import tensorflow as tf
from PIL import Image
import numpy as np
import os
img_path = 'path/to/folder/image.bmp'
image_res = [512,512]
def preprocess(img_path):
img_read = tf.read_file(img_path)
img_decode = tf.image.decode_bmp(img_read, channels=0)
img_reshape = tf.expand_dims(img_decode,0)
img_resize = tf.image.resize_bilinear(img_reshape,size=image_res,align_corners=False)
img_final = tf.squeeze(img_resize,[0]) # Use to push to tf.keras model
return img_final
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init_op)
res_v1 = sess.run(preprocess(img_path))
res_v1 = res_v1.astype(np.uint8)
img_arr_v1 = Image.fromarray(np.squeeze(img_uint8_v1),'L')
img_arr_v1.save("path/to/output/folder/res_bilinear.jpeg")
希望对您有所帮助。
除了下面这个小程序的垃圾输出,我什么也得不到。我只想
- 加载并解码 jpeg 图像
- 使用
tf.resize_bilinear
将其大小调整为 (224, 224)
- 将其重新编码为 jpeg 并保存到文件
将 tensorflow 导入为 tf
将 numpy 导入为 np
进口os
从 PIL 导入图片
cur_dir = os.getcwd()
print("resizing images")
print("current directory:",cur_dir)
def modify_image(image):
resize_shape = tf.stack([224, 224])
resize_shape_as_int = tf.cast(resize_shape, dtype=tf.int32)
#resized = tf.image.resize_bilinear(decoded_image_4d, resize_shape_as_int)
resized = tf.image.resize_images(image, resize_shape_as_int)
#image_3d = tf.squeeze(resized, squeeze_dims=[0])
image_3d = tf.image.convert_image_dtype(resized, tf.uint8, saturate=False)
return image_3d
def read_image(filename_queue):
reader = tf.WholeFileReader()
key,value = reader.read(filename_queue)
image = tf.image.decode_jpeg(value)
return key,image
def inputs(args):
filenames = args.input_files
filename_queue = tf.train.string_input_producer(filenames)
filename,read_input = read_image(filename_queue)
reshaped_image = modify_image(read_input)
img = tf.image.encode_jpeg(reshaped_image)
return filename,img
def parse_args():
a = argparse.ArgumentParser()
a.add_argument('input_files', nargs='+')
args = a.parse_args()
return args
def main():
args = parse_args()
with tf.Graph().as_default():
image = inputs(args)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
tf.train.start_queue_runners(sess=sess)
filename,img = sess.run(image)
with open(os.path.join(cur_dir, 'output.jpg'), 'wb') as fh:
fh.write(img)
if __name__ == '__main__':
main()
虽然我得到的只是垃圾数据,但输出的是这样的东西
tf.stack
接收 Tensor
个对象的列表而不是整数。
迟到的答案。这是一个解决方案,
import tensorflow as tf
from PIL import Image
import numpy as np
import os
img_path = 'path/to/folder/image.bmp'
image_res = [512,512]
def preprocess(img_path):
img_read = tf.read_file(img_path)
img_decode = tf.image.decode_bmp(img_read, channels=0)
img_reshape = tf.expand_dims(img_decode,0)
img_resize = tf.image.resize_bilinear(img_reshape,size=image_res,align_corners=False)
img_final = tf.squeeze(img_resize,[0]) # Use to push to tf.keras model
return img_final
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init_op)
res_v1 = sess.run(preprocess(img_path))
res_v1 = res_v1.astype(np.uint8)
img_arr_v1 = Image.fromarray(np.squeeze(img_uint8_v1),'L')
img_arr_v1.save("path/to/output/folder/res_bilinear.jpeg")
希望对您有所帮助。