I want to read a file as binary data and convert the read binary data into a 1-bit array.
For example, a file called a.png is read as a binary file and the data is arranged in 1-bit arrays
(Example) [0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, ...]
I want to do something, but I don't know how.
I would appreciate it if you could tell someone.

Supplemental information


  • Answer # 1

    Reference information

    Handling binary data in Python | Reading and hexadecimal display

    Python binary data

    Reviewing Python3 (binary data)

    I wrote this code.

    file_path = "a.txt"
    data = []
    with open (file_path, 'rb') as f:
        data = f.read ()
    for d in data:
        print (d)
        print (list (map (lambda x: int (x), format (d, '08b'))))

    Execution example

  • Answer # 2

    read byte by byte I don't know if the value is the most significant or least significant value, but it takes the bit pattern and bit AND in order from the end, and adds 0 or 1 to the list depending on the result.

    Or, make the byte value a binary string withbin (), make it an 8-digit binary string, thenlist ()Do you want to add it?