Python3, binary data diffrent representation from what i need

Question:

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

b'x89PNGrnx1anx00x00x00rIHDRx00x00x00x01x00x00x00x01x01x03x00x00x00%xdbVxcax00x00x00x03PLTEx00x00x00xa7z=xdax00x00x00x01tRNSx00@xe6xd8fx00x00x00nIDATx08xd7c`x00x00x00x02x00x01xe2!xbc3x00x00x00x00IENDxaeB`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

Asked By: Riccardo Piana

||

Answers:

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

binaryData.hex()

returns

'89504e470d0a1a0a0000000d494844520000000100000001010300000025db56ca00000003504c5445000000a77a3dda0000000174524e530040e6d8660000000a4944415408d76360000000020001e221bc330000000049454e44ae426082'
Answered By: Nuri Taş