python - 在 Python 中读取原始二进制图像

标签 python image numpy binary

我在 Matlab 中有一个非常简单的脚本,它可以打开一个“原始”二进制图像文件并显示它。这在 python 中使用 numpy 很容易重现吗?我看过各种讨论解包、处理字节序、指定缓冲区等的帖子。但这看起来应该很简单,基于 matlab 接口(interface)的简单程度

>> fileID = fopen('sampleX3.raw','rb')

fileID =

     1

>> A = fread(fileID,[1024,1024],'int16');
size(A)

ans =

        1024        1024

>> max(max(A))

ans =

       12345

>> close all; figure; imagesc(A);

最佳答案

这将使用 numpy 和 matplotlib 做同样的事情:

import numpy as np
from matplotlib import pylab as plt

A = np.fromfile(filename, dtype='int16', sep="")
A = A.reshape([1024, 1024])
plt.imshow(A)

我觉得有必要提一下,使用原始二进制文件存储数据通常不是一个好主意。

关于python - 在 Python 中读取原始二进制图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17479296/

相关文章:

python - 使用pdb以简单的方式调试python代码

python - 使用 numpy/scipy 在 python 中进行傅里叶变换/迭代反卷积拟合

python - 系列的平均值,跳过元素[0]

python - python 创建偏移量列表

python - 谷歌存储上 bucket() 和 get_bucket() 的区别

c# - 保存加载UWP的文件夹路径

css - 对图像使用媒体查询

python - 大文件保管箱上传 APIv2 python

python - 使用 sklearn IterativeImputer 时,出现 ValueError : Input contains NaN, 无穷大或值对于 dtype ('float64' 来说太大

android - 突出显示图像上的文字,类似于谷歌翻译应用程序