6

I have a very simple script in Matlab that opens a 'raw' binary image file and displays it. Is this easily reproducible using numpy in python? I've come across various posts discussing unpacking, dealing with endian, specifying buffers, etc. But this seems like it should be simple, based on how simple the matlab interface is

>> 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
  • Did you try openCV? it designed for computer vision... Commented Jul 4, 2013 at 23:44

1 Answer 1

11

This will do the same thing using numpy and 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)

I feel obligated to mention that using raw binary files to store data is generally a bad idea.

Sign up to request clarification or add additional context in comments.

2 Comments

Why do you say that store data in a binary file is a bad idea?
The original question had a square image, but keep in mind MATLAB uses row-major order and python uses column-major. You have to swap the numbers in the reshape function in Python and sizeA parameter given to fread in MATLAB.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.