Using Python to read and edit bitmaps

I need to read a bitmap file (.bmp) and split the binary data into an array of bytes, which I can then restore to the original file. In the end, I will modify part of each byte to store data, and then read it to return data.

More details

I am currently using

file = open ("example.bmp", "rb")

data = file.read ()

file.close ()

to get the data. However, it is rather slow and inefficient. Then I want to split it into a byte array and change the last bit to 0 for each bit that is not part of the metadata (I will use if to subtract 1 from each odd byte). Then I re-merge the data and save it in a new image file with the following code:

file = open ("example2.bmp", "wb")

file.write (data)

file.close ()

although I suspect this may not be optimal either.

I need to know how to split a lot of binary data into bytes.

+4
source share
1 answer

data already a byte array that can be indexed using slice notation. For example, according to the BMP file, the Bitmap File header is in data[0:14] . You can use the C libraries in Python instead to save some time.

+5
source

Source: https://habr.com/ru/post/1485854/


All Articles