Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用tensorflow读取.bmp图像并写入磁盘?_Python_Tensorflow - Fatal编程技术网

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

Python 如何使用tensorflow读取.bmp图像并写入磁盘?,python,tensorflow,Python,Tensorflow,我正在尝试读取bmp图像(2048x2048),将其大小调整为256x256,并使用tensorflow将图像写入磁盘。我已经成功地读取了它,但找不到将其写入磁盘的方法。知道怎么做吗 代码如下: 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 IM

我正在尝试读取bmp图像(2048x2048),将其大小调整为256x256,并使用tensorflow将图像写入磁盘。我已经成功地读取了它,但找不到将其写入磁盘的方法。知道怎么做吗

代码如下:

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.


ValueError回溯(最近一次调用上次)
D:\Users\ge3f-P2\Anaconda3\lib\site packages\tensorflow\python\framework\op_def_library.py in\u apply\u op_helper(self,op_type\u name,name,**关键字)
509 as_ref=输入参数为,
-->510首选类型(默认类型)
511除类型错误作为错误外:
D:\Users\ge3f-P2\Anaconda3\lib\site packages\tensorflow\python\framework\ops.py在内部\u convert\u to \u tensor(值、数据类型、名称、as\u ref、首选\u数据类型、ctx)
1145如果ret为无:
->1146 ret=conversion\u func(值,dtype=dtype,name=name,as\u ref=as\u ref)
1147
D:\Users\ge3f-P2\Anaconda3\lib\site packages\tensorflow\python\framework\ops.py in\u TensorTensorConversionFunction(t,dtype,name,as\u ref)
982“对于数据类型为%s的张量,张量转换请求的数据类型为%s:%r”%
-->983(dtype.name,t.dtype.name,str(t)))
984返回t
ValueError:Tensor转换请求了数据类型为uint8的Tensor的数据类型字符串:“Tensor(“DecodeBmp:0”,shape=(?,?,1),dtype=uint8)”
在处理上述异常期间,发生了另一个异常:
TypeError回溯(最近一次调用上次)
在里面
---->1文件=tf.写入文件(文件名,最终)
D:\Users\ge3f-P2\Anaconda3\lib\site packages\tensorflow\python\ops\gen\u io\u ops.py(文件名、内容、名称)
2256如果_ctx为None或not _ctx._eager_context.is eager:
2257 u,u,_op=_op_def_lib._apply_op_helper(
->2258“WriteFile”,filename=filename,contents=contents,name=name)
2259返回操作
2260_结果=无
D:\Users\ge3f-P2\Anaconda3\lib\site packages\tensorflow\python\framework\op_def_library.py in\u apply\u op_helper(self,op_type\u name,name,**关键字)
531如果输入参数类型!=类型_pb2.DT_无效:
532 raise TypeError(“%s预期类型为%s.”%
-->533(前缀,数据类型.as\u数据类型(输入参数类型).name))
534其他:
535#如果需要,用默认值更新地图。
TypeError:“WriteFile”Op的输入“contents”的类型uint8与预期的字符串类型不匹配。
问题是我找不到一个“encode_bmp”或任何与bmp相关的函数可以用来编码图像并将调整大小的图像保存到磁盘

我读了这篇文章,但这无助于解决问题。

由于Tensorflow目前没有将图像保存/编码为BMP格式的本机方法,解决此问题的一种方法是将图像保存为临时位置的PNG,然后使用Python图像库将其转换为BMP

请参阅:和列表

据我所知,您收到异常的原因是您试图保存
unit8
张量,而
write\u file
方法需要一个-encoded-string

试试这个:

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)

我相信您丢失了一些代码<代码>img_4d未定义。您链接的问题的答案已经指出tensorflow无法保存.bmp文件。是否有特殊原因,或者您是否也可以使用.png?@SteffenWinkler我已经编辑了代码。现在定义了
img_4d
。我的要求不允许使用除bmp以外的任何其他格式。我发现没有办法对bmp进行编码:(你看完这个答案了吗?