Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a binary file into 2D array python

Tags:

python

numpy

I am having trouble reading a binary file in python and plotting it. It is supposedly an unformatted binary file representing a 1000x1000 array of integers. I have used:

image = open("file.dat", "r")
a = np.fromfile(image, dtype=np.uint32)

Printing the length returns 500000. I cannot figure out how to create a 2D array out of it.

like image 205
swisswilson Avatar asked Oct 18 '25 14:10

swisswilson


1 Answers

Since you are getting half a million uint32s using

a = np.fromfile(image, dtype=np.uint32) 

then you will get a million uint16s using

a = np.fromfile(image, dtype=np.uint16) 

There are other possibilities, however. The dtype could be any 16-bit integer dtype such as

  • >i2 (big-endian 16-bit signed int), or
  • <i2 (little-endian 16-bit signed int), or
  • <u2 (little-endian 16-bit unsigned int), or
  • >u2 (big-endian 16-bit unsigned int).

np.uint16 is the same as either <u2 or >u2 depending on the endianness of your machine.


For example,

import numpy as np
arr = np.random.randint(np.iinfo(np.uint16).max, size=(1000,1000)).astype(np.uint16)
arr.tofile('/tmp/test')
arr2 = np.fromfile('/tmp/test', dtype=np.uint32)
print(arr2.shape)
# (500000,)

arr3 = np.fromfile('/tmp/test', dtype=np.uint16)
print(arr3.shape)
# (1000000,)

Then to get an array of shape (1000, 1000), use reshape:

arr = arr.reshape(1000, 1000)
like image 147
unutbu Avatar answered Oct 20 '25 03:10

unutbu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!