user3238361
user3238361

Reputation: 15

How to remove a header from a file that is bmp but with no import of any libraries in python

How to remove a header from a file that is .bmp but with no import of any libraries in python and reading byte byte f.read(1)?

Upvotes: 0

Views: 4871

Answers (3)

Darren
Darren

Reputation: 11

Use a hex editor, remove the header file bytes and save the altered file - minus the header. There are also a few bytes at the end of a Bitmap file to remove if you are being thorough. Try XVI32 hex editor or HxD. Both are very good hex editors and they are FREE. Good luck.

Upvotes: 1

Gexos
Gexos

Reputation: 3

BMP isn't a text file format, it's a binary format. That means you have to read it in binary mode. And you can't read it "line by line", because it doesn't have lines of text to read. Since a bytes object isn't mutable, you will probably want to copy it into a bytearray to work with. So:

 with open('spam.bmp', 'rb') as f:
    data = bytearray(f.read())

Upvotes: 0

Claudiu
Claudiu

Reputation: 229421

You have to look up the BMP file format and use it to figure out how to parse the header.

According to this, the header starts as follows:

Offset# Size    Purpose
0000h   2 bytes the header field used to identify the BMP & DIB file is 0x42 0x4D in hexadecimal, same as BM in ASCII. [...]
0002h   4 bytes the size of the BMP file in bytes
0006h   2 bytes reserved; actual value depends on the application that creates the image
0008h   2 bytes reserved; actual value depends on the application that creates the image
000Ah   4 bytes the offset, i.e. starting address, of the byte where the bitmap image data (pixel array) can be found.

So what you want to do is read the bytes at offsets 10-13, parse them as a 4-byte integer, and that integer represents where in the file to seek to get all of the image data. Then you just have to read all the image data and put it in another file. I am not sure why you would want to do this, though, since without the header it will be extremely difficult to tell what format the image data is in.

Upvotes: 2

Related Questions