使用tensorflow加载,解码,resize_bilinear(),然后编码和编写jpeg图像的工作示例?

问题描述 投票:1回答:1

我从来没有得到任何东西,只有下面这个小程序的垃圾输出。我想做的就是

  1. 加载和解码jpeg图像
  2. 使用tf.resize_bilinear将其调整为(224,224)
  3. 将其重新编码为jpeg并将其保存到文件中

导入张量流为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()

我只是得到垃圾数据,这样的输出

enter image description here

tensorflow jpeg bilinear-interpolation
1个回答
0
投票

tf.stack列出了Tensor对象而不是整数。

© www.soinside.com 2019 - 2024. All rights reserved.