在Python中读入原始二进制图像

时间:2013-07-04 23:39:16

标签: python image numpy binary

我在Matlab中有一个非常简单的脚本,可以打开一个“原始”二进制图像文件并显示它。使用python中的numpy可以轻松重现吗?我遇到过各种各样的帖子,讨论解包,处理endian,指定缓冲区等等。但这似乎应该很简单,基于matlab接口的简单性

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

1 个答案:

答案 0 :(得分:7)

这将使用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)

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