简体   繁体   English

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

[英]How to read .bmp images using tensorflow and write to disk?

I am trying to read bmp images (2048 x2048), resize them to 256x 256 and write the image to disk using tensorflow. 我正在尝试读取bmp图像(2048 x2048),将它们的大小调整为256x 256,然后使用tensorflow将图像写入磁盘。 I have succeeded in reading it but unable to find a way write it to disk. 我已成功读取它,但是找不到将其写入磁盘的方法。 Any idea how to do it ? 知道怎么做吗?

Here is the code below: 这是下面的代码:

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()

Error: 错误:


        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.

The problem is I can't find a "encode_bmp" or any bmp related function that can be used to encode the image and save the resized image to disk. 问题是我找不到“ encode_bmp”或任何与bmp相关的函数,可用于对图像进行编码并将调整大小后的图像保存到磁盘。

I went through this thread but this doesn't help solve the question. 我经历了这个线程,但这无助于解决问题。 Link here 连结这里

Since Tensorflow currently does not have a native way of saving/encoding images to the BMP format, one way to solve this would be to save the image as a PNG in a temporary location and then use the Python Imaging Library to convert it to a BMP. 由于Tensorflow当前没有将图像保存/编码为BMP格式的本地方法,因此解决此问题的一种方法是将图像另存为PNG到临时位置,然后使用Python Imaging Library将其转换为BMP 。

See: PILs Image.Save method and the list of supported file formats . 请参阅: PIL Image.Save方法支持的文件格式列表。

From my understanding, the reason for the exception you are receiving is that you are trying to save a unit8 tensor, when the write_file method expects an - encoded - string. 据我了解,您收到异常的原因是,当write_file方法需要一个-编码-字符串时,您试图保存一个unit8张量。

Try this: 尝试这个:

from PIL import Image
.
.
.
file_name = "D:/tmp.png"
enc = tf.image.encode_png(img_res)
file = tf.write_file(file_name, enc)
print('PNG Image Saved')
session.close()
Image.open(file_name).save("D:/out.bmp")
os.remove(file_name)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM