Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read/Write binary file
#1
I have a binary file that contains 32 bit floats. I would like to read the file into a matrix, rotate the matrix, and then write the matrix to a file.

In the original file the values would be arranged like this:
a0, b0, a1, b1, a2, b2, a3, b3...aN, bN
where a,b can be thought of as rows and 1, 2, 3 as columns.

In the resulting file the values would be arranged like this:
a0, a1, a2, a3...aN, b0, b1, b2, b3...bN

I'm beginning to think I might need to use the array library to read and write the files, and write some C code to do the rotation. Does anyone know of other ways?
Reply
#2
I don't think it's nearly as complicated as you think.
Please supply a sample file.
Reply
#3
I don't know how to provide a sample file, so here is some code to generate the sample file and demonstrate the kind of manipulation I need.
import numpy as np
from array import array

a = array('f', [x for _ in range(10) for x in range(4)])
with open('sample_order.bin', 'wb') as file:
    a.tofile(file)

a = array('f')
with open('sample_order.bin', 'rb') as file:
    a.fromfile(file, 40)
print('Sample order file', a, sep='\n')

b = np.array(a)
print('As numpy array', b, sep='\n')
c = b.reshape((10, 4))
print('As a matrix', c, sep='\n')
d = c.transpose()
print('As matrix in channel order', d, sep='\n')
e = array('f', d.flatten())
print('Channel order file', e, sep='\n')

with open('channel_order.bin', 'wb') as file:
    e.tofile(file)
I start with an array like this:
0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3

I want to convert this to:
0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3

I am doing this by turning it into a matrix, transposing the matrix and then flattening the matrix, I know there has to be a better way.
Reply
#4
for 2d array, I'd use something like below

But you have a single dimension array, so you're way ahead of me on this.

import numpy as np
from scipy.ndimage.interpolation import rotate
import os

# make sure file can be found in script directory
os.chdir(os.path.abspath(os.path.dirname(__file__)))

def rotate_and_save(infilename, outfilename, rotation_angle_degrees=90):    
    orig_array = np.fromfile(infilename, dtype=np.int64)
    print(f"\noriginal array\n{orig_array}")
    new_array = rotate(orig_array, angle=rotation_angle_degrees)
    print(f"\nnew array\n{new_array}")
    np.tofile(outfilename, dtype=np.int64)

def main():
    # Use any desired angle
    rotate_and_save('channel_order.bin', 'rotated_channel_order.bin', 
        rotation_angle_degrees=180)

if __name__ == '__main__':
    main(
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? JohnJSal 12 27,894 Feb-13-2025, 04:48 AM
Last Post: tomhansky
  How to write variable in a python file then import it in another python file? tatahuft 4 881 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  How to read a file as binary or hex "string" so that I can do regex search? tatahuft 3 1,012 Dec-19-2024, 11:57 AM
Last Post: snippsat
  [SOLVED] [Linux] Write file and change owner? Winfried 6 1,491 Oct-17-2024, 01:15 AM
Last Post: Winfried
  python read PDF Statement and write it into excel mg24 1 942 Sep-22-2024, 11:42 AM
Last Post: Pedroski55
  Read TXT file in Pandas and save to Parquet zinho 2 1,210 Sep-15-2024, 06:14 PM
Last Post: zinho
  Pycharm can't read file Genericgamemaker 5 1,545 Jul-24-2024, 08:10 PM
Last Post: deanhystad
  Python is unable to read file Genericgamemaker 13 3,590 Jul-19-2024, 06:42 PM
Last Post: snippsat
  Delete file with read-only permission, but write permission to parent folder cubei 6 25,304 Jun-01-2024, 07:22 AM
Last Post: Eleanorreo
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 3,231 May-03-2024, 07:23 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020