gpt4 book ai didi

python - 将 Python 字节转换为字符串而不进行编码

转载 作者:行者123 更新时间:2023-11-30 22:17:26 30 4
gpt4 key购买 nike

我使用的是 Python 3.6,并且我有一个字节图像:

img = b'\xff\xd8\xff\xe0\x00\x10JFIF\x00'

我需要将字节转换为字符串而不进行编码,因此它看起来像:

raw_img = '\xff\xd8\xff\xe0\x00\x10JFIF\x00'

目标是将其合并到 html 图像标签中:

<img src="'data:image/png;base64," + base64.b64encode(raw_img) + "' />"

最佳答案

为什么不直接调用 str 并删除后面的 b 呢?

在:

str(img)[2:-1]

输出:

'\xff\xd8\xff\xe0\x00\x10JFIF\x00'

关于python - 将 Python 字节转换为字符串而不进行编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49677790/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com