Home>

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.
Please

Supplemental information

python3.6

  • Answer # 1

    Reference information

    Handling binary data in Python | Reading and hexadecimal display
    https://qiita.com/MasaEguchi/items/0fd5a8292cd9d64e284a

    Python binary data
    http://www.ajisaba.net/python/binary.html

    Reviewing Python3 (binary data)
    https://kanitamago5503.hatenablog.com/entry/2019/01/24/224041

    I wrote this code.
    b.py

    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?