Python3, binary data diffrent representation from what i need

I have to add an image to a database, so I open the image as binary and it stores it this way:

b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x01\x03\x00\x00\x00%\xdbV\xca\x00\x00\x00\x03PLTE\x00\x00\x00\xa7z=\xda\x00\x00\x00\x01tRNS\x00@\xe6\xd8f\x00\x00\x00\nIDAT\x08\xd7c`\x00\x00\x00\x02\x00\x01\xe2!\xbc3\x00\x00\x00\x00IEND\xaeB`\x82'

However I need it to be strored this way:

0x89504e470d0a1a0a0000000d494844520000000100000001010300000025db56ca00000003504c5445000000a77a3dda0000000174524e530040e6d8660000000a4944415408d76360000000020001e221bc330000000049454e44ae426082

It is my first ever time working with binary files so there is probably something basic I’m not understanding.

This is my code for opening the image in python:

with open("1x1.jpg", 'rb') as File:
    binaryData=File.read()
    print(binaryData)

This is the image: (1×1 empty pixel, I changed the extension from png to jpg, the original image is from https://upload.wikimedia.org/wikipedia/commons/c/ca/1×1.png)
1x1 empty pixel

>Solution :

binaryData is bytes and you need to convert it to the hex format.

binaryData.hex()

returns

'89504e470d0a1a0a0000000d494844520000000100000001010300000025db56ca00000003504c5445000000a77a3dda0000000174524e530040e6d8660000000a4944415408d76360000000020001e221bc330000000049454e44ae426082'

Leave a Reply