在python中显示二进制文件中的数据

在python中显示二进制文件中的数据,python,python-3.x,numpy,matplotlib,Python,Python 3.x,Numpy,Matplotlib,我有2000个图像存储为一个二进制文件“file.dat”,这个文件有一个512字节的头。每个图像的格式为512*512*2字节(无符号整数16)。我的任务是将所有这些图像可视化为视频。如何在python中实现这一点?我的问题是从阅读图像序列开始。我是python的新手。Numpy非常适合阅读简单的二进制文件格式 从它的声音,你有一个大的uin16的二进制文件,你想读入一个3D阵列和可视化。我们不必将其全部加载到内存中,但对于本例,我们将 下面是代码的基本概念: import numpy as

我有2000个图像存储为一个二进制文件“file.dat”,这个文件有一个512字节的头。每个图像的格式为512*512*2字节(无符号整数16)。我的任务是将所有这些图像可视化为视频。如何在python中实现这一点?我的问题是从阅读图像序列开始。我是python的新手。

Numpy非常适合阅读简单的二进制文件格式

从它的声音,你有一个大的uin16的二进制文件,你想读入一个3D阵列和可视化。我们不必将其全部加载到内存中,但对于本例,我们将

下面是代码的基本概念:

import numpy as np
import matplotlib.pyplot as plt

def main():
    data = read_data('test.dat', 512, 512)
    visualize(data)

def read_data(filename, width, height):
    with open(filename, 'r') as infile:
        # Skip the header
        infile.seek(512)
        data = np.fromfile(infile, dtype=np.uint16)
    # Reshape the data into a 3D array. (-1 is a placeholder for however many
    # images are in the file... E.g. 2000)
    return data.reshape((width, height, -1))

def visualize(data):
    # There are better ways to do this, but let's keep it simple
    plt.ion()
    fig, ax = plt.subplots()
    im = ax.imshow(data[:,:,0], cmap=plt.cm.gray)
    for i in xrange(data.shape[-1]):
        image = data[:,:,i]
        im.set(data=image, clim=[image.min(), image.max()])
        fig.canvas.draw()

main()

python有opencv绑定。我会从那里开始