0

I do have a binary file (raw image), and I read it with a specific pattern as following code, I now want to process the np array (new_array), so after processing the new_array, how do I convert it back to the array and save it to the new binary file (raw image)?

f = np.fromfile(path, dtype=np.uint16)

new_array = np.zeros(cols * rows, dtype=int)
for i in range(0, cols * rows):
    tmp = '{0:08b}'.format(f[i])
    part1 = tmp[4:8]
    part2 = tmp[0:4]
    new_array[i] = (int(part1,2)<<2) + (int(part2,2)>>6)

res_array = new_array.reshape((rows, cols))

The pattern is given above. I am just wondering how to convert res_array to the type of the original one (the variable f) and save it to a new binary file.

martineau
  • 112,593
  • 23
  • 157
  • 280

1 Answers1

0

I think you could flatten the np array (res_array) and write the result np array to a binary file.

Flattening the array will "convert it back" a np.uint16 one-dimensional array. Then, writing the binary file should dump the raw results without encoding them.

SpaceKatt
  • 848
  • 6
  • 12