python - 如何使用 tensorflow 读取 .bmp 图像并写入磁盘?
问题描述
我正在尝试读取 bmp 图像(2048 x2048),将它们调整为 256x 256 并使用 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.
问题是我找不到“encode_bmp”或任何可用于编码图像并将调整大小的图像保存到磁盘的 bmp 相关函数。
我经历了这个线程,但这无助于解决问题。 链接在这里
解决方案
由于 Tensorflow 目前没有将图像保存/编码为 BMP 格式的本地方法,解决此问题的一种方法是将图像另存为 PNG 在临时位置,然后使用 Python 图像库将其转换为 BMP .
请参阅:PILs Image.Save 方法和支持的文件格式列表。
据我了解,您收到异常的原因是您试图保存一个unit8
张量,而该write_file
方法需要一个 - 编码的 - 字符串。
尝试这个:
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)
推荐阅读
- typescript - 如何在 eslint-plugin-import 中为 typescript 配置 `no-cycle` 规则?
- php - Docker 长时间运行的命令内存消耗随时间增长
- ios - XCODE中如何修改第三方框架的Info,plist(使用CocoaPods导入)?
- regex - 用 Lua 匹配变量字符串
- phoenix-live-view - 如何防止 Phoenix Live View 删除由 JavaScript 放置的 DOM 元素?
- javascript - JavaScript 中有字节大小的数据类型吗?
- amazon-web-services - 如何使用 cognito 验证我的 api 网关?
- html - 为在线视频课程创建产品页面的最简单的分步方法是什么?
- javascript - ijavascript / Jupyter Notebook JavaScript - 如何在没有错误的情况下重新运行单元格(就像使用 Python 一样)
- sas - 如何在 SAS 中处理字符串