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: (1x1 empty pixel, I changed the extension from png to jpg, the original image is from https://upload.wikimedia.org/wikipedia/commons/c/ca/1x1.png) 1x1 empty pixel

推荐答案

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

binaryData.hex()

returns

'89504e470d0a1a0a0000000d494844520000000100000001010300000025db56ca00000003504c5445000000a77a3dda0000000174524e530040e6d8660000000a4944415408d76360000000020001e221bc330000000049454e44ae426082'

Python相关问答推荐

Select 用a和i标签包裹的复选框?

将特定列信息移动到当前行下的新行

如何在箱形图中添加绘制线的传奇?

使用索引列表列表对列进行切片并获取行方向的向量长度

如何使用html从excel中提取条件格式规则列表?

Python库:可选地支持numpy类型,而不依赖于numpy

在Wayland上使用setCellWidget时,try 编辑QTable Widget中的单元格时,PyQt 6崩溃

从一个系列创建一个Dataframe,特别是如何重命名其中的列(例如:使用NAs/NaN)

根据列值添加时区

使用Python从URL下载Excel文件

在嵌套span下的span中擦除信息

Pandas Data Wrangling/Dataframe Assignment

如何防止Pandas将索引标为周期?

获取PANDA GROUP BY转换中的组的名称

如何获取包含`try`外部堆栈的`__traceback__`属性的异常

具有不匹配列的2D到3D广播

查找数据帧的给定列中是否存在特定值

如何在Python中实现高效地支持字典和堆操作的缓存?

使用Scikit的ValueError-了解

Pandas:根据相邻行之间的差异过滤数据帧