python - Reading binary file in two dimension -


matlab code:

binary = 'binaryfile.png'; fid = fopen(binary , 'rb');  data2d(:,:) = fread(fid, [10,10], ['int16=>','double']) 

i looking python equivalent of above code. can use numpy.fromfile function read doesn't allows me read in 2 dimension. or idea how can 1 in python.

although possible want, wouldn't in python because don't need to. want this:

binary = 'binaryfile.png' data2d = np.fromfile(binary, count=10*10, dtype='int16').reshape(10, 10).astype('double') 

reshape in numpy takes no time , no memory, unlike matlab expensive operation. because in numpy reshape doesn't copy data in matlab, different way of looking @ same underlying data.

so in numpy, best way want do. matlab functionality looking workaround limitations in matlab language. numpy doesn't have limitations, allowing simpler function. considered programming practice split tasks series of simple, well-defined operations rather put in single function. due limitations in language, tends lot harder in matlab in python, resulting in matlab having more complicated functions.

now, said, possible want, uglier , harder read without real advantage advise use above approach. here how same thing in numpy:

binary = 'binaryfile.png' data2d = np.fromfile('binaryfile.png', count=10, dtype=('int16', 10)).astype('double') 

this telling read in length-10 int16 row 10 times. interestingly, @ least in tests, approach slower first approach described, due more complexity when doing reading. reading files disk slow, makes more complex hurt performance.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -