What I am really doing is creating a BMP file from JPEG using python and it's got some header data which contains info like size, height or width of the image, so basically I want to read a JPEG file, gets it width and height, calculate the new size of a BMP file and store it in the header.
Let's say the new size of the BMP file is 40000 bytes whose hex value is 0x9c40, now as there is 4 byte space to save this in the header, we can write it as 0x00009c40. In BMP header data, LSB is written first and then MSB so I have to write, 0x409c0000 in the file.
My Problems:-
I was able to do this in C but I am totally lost how to do so in Python.
For example, if I have
i=40000, and by usingstr=hex(i)[2:]I got the hex value, now by some coding I was able to add the extra zeros and then reverse the code. Now how to write this'409c0000'data in the file as hex?The header size is 54 bytes for BMP file, so is there is another way to just store the data in a string like
str='00ffcf4f...'(upto 54 bytes) and just convert the whole str at once as hex and write it to file?My friend told me to use
unhexlifyfrombinascii, by doingunhexlify('fffcff')I get'\xff\xfc\xff'which is what I want but when I tryunhexlify('3000')I get '0\x00'` which is not what I want. It is same for any value containing 3, 4, 5, 6 or 7. Is it the right way to do this?
convertprogram from the ImageMagick suite.