4

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.

1
  • If it is binary you should open it with rb Commented Mar 31, 2015 at 23:42

1 Answer 1

5

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)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.