如何使用张量流读取.bmp图像并写入磁盘?

时间:2019-06-26 12:29:05

标签: python tensorflow

我正在尝试读取bmp图像(2048 x2048),将它们的大小调整为256x 256,然后使用张量流将图像写入磁盘。我已成功读取它,但是找不到将其写入磁盘的方法。你知道怎么做吗?

这是下面的代码:

import tensorflow as tf

img_path = "D:/image01.bmp"

img = tf.read_file(img_path)

img_decode = tf.image.decode_bmp(img, channels=1) # unit8 tensor

IMG_WIDTH = 256

IMG_HEIGHT = 256

img_cast = tf.cast(img_decode,dtype=tf.uint8)

img_4d = tf.expand_dims(img_cast, axis=0)

img_res = tf.image.resize_bilinear(img_4d, (IMG_HEIGHT, IMG_WIDTH), align_corners=True)

session = tf.InteractiveSession()

file_name = "D:/out.bmp"

file = tf.write_file(file_name, img_res)

print('Image Saved')

session.close()

错误:


        ValueError                                Traceback (most recent call last)
    D:\Users\ge3f-P2\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
        509                 as_ref=input_arg.is_ref,
    --> 510                 preferred_dtype=default_dtype)
        511           except TypeError as err:

    D:\Users\ge3f-P2\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, ctx)
       1145     if ret is None:
    -> 1146       ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
       1147 

    D:\Users\ge3f-P2\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in _TensorTensorConversionFunction(t, dtype, name, as_ref)
        982         "Tensor conversion requested dtype %s for Tensor with dtype %s: %r" %
    --> 983         (dtype.name, t.dtype.name, str(t)))
        984   return t

    ValueError: Tensor conversion requested dtype string for Tensor with dtype uint8: 'Tensor("DecodeBmp:0", shape=(?, ?, 1), dtype=uint8)'

    During handling of the above exception, another exception occurred:

    TypeError                                 Traceback (most recent call last)
    <ipython-input-18-9b7aeb9e42de> in <module>
    ----> 1 file = tf.write_file(file_name,final)

    D:\Users\ge3f-P2\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_io_ops.py in write_file(filename, contents, name)
       2256   if _ctx is None or not _ctx._eager_context.is_eager:
       2257     _, _, _op = _op_def_lib._apply_op_helper(
    -> 2258         "WriteFile", filename=filename, contents=contents, name=name)
       2259     return _op
       2260     _result = None

    D:\Users\ge3f-P2\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
        531             if input_arg.type != types_pb2.DT_INVALID:
        532               raise TypeError("%s expected type of %s." %
    --> 533                               (prefix, dtypes.as_dtype(input_arg.type).name))
        534             else:
        535               # Update the maps with the default, if needed.

    TypeError: Input 'contents' of 'WriteFile' Op has type uint8 that does not match expected type of string.

问题是我找不到“ encode_bmp”或任何与bmp相关的功能,可用于对图像进行编码并将调整大小后的图像保存到磁盘。

我经历了这个线程,但这无助于解决问题。 Link here

1 个答案:

答案 0 :(得分:1)

由于Tensorflow当前没有将图像保存/编码为BMP格式的本机方法,因此解决此问题的一种方法是将图像另存为PNG,然后放置在临时位置,然后使用Python Imaging Library对其进行转换到BMP。

请参阅:PILs Image.Save methodsupported file formats的列表。

据我了解,您收到异常的原因是,当value方法需要一个编码字符串时,您试图保存一个unit8张量。

尝试一下:

write_file