python - 如何将 numpy 数组放入与 open().read() 相同的字节对象中?
问题描述
我最初在这里发布了这个问题: https ://community.plot.ly/t/images-are-not-displayed-after-altering/26521/2 但我觉得它更适合stackoverflow。所以它基本上归结为我试图弄清楚我如何获得当我打开一个 .png 文件时创建的相同字节对象
with open('temp.png', 'rb') as f:
encoded=base64.b64encode(f.read())
在我已经将图像打开为 numpy 数组之后。当我只是尝试将 numpy 数组转换为字节对象时,它看起来与我使用 open 得到的完全不同:
(io.BytesIO(imageio.imread(img_path))).getvalue()[0:20]
Out[70]: b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
(imageio.imread(img_path)).tobytes()[0:20]
Out[71]: b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
(open(img_path, 'rb').read())[0:20]
Out[72]: b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x04\x00'
并且具有不同的长度(np 数组的转换要长一个数量级)。这些方法之间有什么区别,在将图像转换为 np.array 后,如何实现 open.read() 的功能?
解决方案
推荐阅读
- java - 如何使用独特的查找表进行连接
- bash - 如何仅grep输出的第一个单词
- javascript - 未捕获的 ReferenceError:未定义自动计算
- java - 如何在 Java 的命令行中创建用户选择菜单?
- python - 数据砖社区版无法读取上传的表(之前工作)
- woocommerce - 如何通过删除“wc_”前缀来操作 WooCommerce 订单键?
- html - 媒体查询没有以不同的大小响应
- javascript - 网站构建器解析脚本时如何将javascript插入html页面
- arrays - Ruby - 更新散列数组的一个散列中的键/值会更新所有散列
- android - 使用音频播放器包时出现颤振错误