Python 无法将tensorflow图像保存到文件

Python 无法将tensorflow图像保存到文件,python,tensorflow,Python,Tensorflow,我需要将图像大小调整到一定大小并保存到文件中,因此我选择了tf.image.resize\u image\u,并使用\u crop\u或\u pad函数: import tensorflow as tf image_decoded = tf.image.decode_jpeg(tf.read_file('1.jpg'), channels=3) cropped = tf.image.resize_image_with_crop_or_pad(image_decoded, 200,

我需要将图像大小调整到一定大小并保存到文件中,因此我选择了tf.image.resize\u image\u,并使用\u crop\u或\u pad函数:

import tensorflow as tf

image_decoded = tf.image.decode_jpeg(tf.read_file('1.jpg'), channels=3)
cropped       = tf.image.resize_image_with_crop_or_pad(image_decoded, 200, 200)
tf.write_file('2.jpg', cropped)
失败,出现错误:

Traceback (most recent call last):
  File "/home/test/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 490, in apply_op
    preferred_dtype=default_dtype)
  File "/home/test/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 669, in convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/home/test/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 583, in _TensorTensorConversionFunction
    % (dtype.name, t.dtype.name, str(t)))
ValueError: Tensor conversion requested dtype string for Tensor with dtype uint8: 'Tensor("control_dependency_3:0", shape=(200, 200, 3), dtype=uint8)'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "train.py", line 15, in <module>
    tf.write_file('2.jpg', cropped)
  File "/home/test/anaconda3/lib/python3.5/site-packages/tensorflow/python/ops/gen_io_ops.py", line 694, in write_file
    contents=contents, name=name)
  File "/home/test/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 508, in apply_op
    (prefix, dtypes.as_dtype(input_arg.type).name))
TypeError: Input 'contents' of 'WriteFile' Op has type uint8 that does not match expected type of string.

我在Linux Mint上使用Tensorflow v0.12.0-rc0,您首先需要将图像从张量编码为jpeg,然后保存它。此外,您应该执行一个会话来评估代码:

import tensorflow as tf

image_decoded = tf.image.decode_jpeg(tf.read_file('1.jpg'), channels=3)
cropped       = tf.image.resize_image_with_crop_or_pad(image_decoded, 200, 200)
enc = tf.image.encode_jpeg(cropped)
fname = tf.constant('2.jpg')
fwrite = tf.write_file(fname, enc)

sess = tf.Session()
result = sess.run(fwrite)
编辑:与TensorFlow 2兼容模式相同

fname = '2.jpg'
with tf.compat.v1.Session() as sess:
    image_decoded = tf.image.decode_jpeg(tf.io.read_file('1.jpg'), channels=3)
    cropped = tf.image.resize_with_crop_or_pad(image_decoded, 200, 200)
    enc = tf.image.encode_jpeg(cropped)
    fwrite = tf.io.write_file(tf.constant(fname), enc)
    result = sess.run(fwrite)

首先需要将图像从张量编码为jpeg,然后保存它。此外,您应该执行一个会话来评估代码:

import tensorflow as tf

image_decoded = tf.image.decode_jpeg(tf.read_file('1.jpg'), channels=3)
cropped       = tf.image.resize_image_with_crop_or_pad(image_decoded, 200, 200)
enc = tf.image.encode_jpeg(cropped)
fname = tf.constant('2.jpg')
fwrite = tf.write_file(fname, enc)

sess = tf.Session()
result = sess.run(fwrite)
编辑:与TensorFlow 2兼容模式相同

fname = '2.jpg'
with tf.compat.v1.Session() as sess:
    image_decoded = tf.image.decode_jpeg(tf.io.read_file('1.jpg'), channels=3)
    cropped = tf.image.resize_with_crop_or_pad(image_decoded, 200, 200)
    enc = tf.image.encode_jpeg(cropped)
    fwrite = tf.io.write_file(tf.constant(fname), enc)
    result = sess.run(fwrite)

我不确定这个函数是否存在。它不在图像大小调整页面上,但有类似的内容。如果你觉得我的答案有用,请接受。我不确定该函数是否存在。它不在图片大小调整页面上,但类似的东西。如果你觉得我的答案有用,请接受它。谢谢!!非常感谢!!