Python3.5 将 BytesIO 或 StringIO 与 base64 结合使用时出错。standard_b64encode

Python3.5 error using BytesIO or StringIO with base64.standard_b64encode

我正在尝试获取 BytesIO 或 StringIO 对象的内容并使用 base64.standard_b64encode() 对其进行编码。我都试过了。这在 python 2.7 中工作正常,但是在 python 3.5 中我得到以下错误。

TypeError: 无法将 'bytes' 对象隐式转换为 str

这是有问题的代码部分。

output = BytesIO()
img.save(output,  format="PNG")
output.seek(0)


data = "data:image/png;base64," + base64.standard_b64encode(output.read())

html = "<html><body><img src='DATA'></body></html>"

我看到过使用 b"sting" 修复字符串错误的参考文献,但我不知道如何将其应用于从文件中读取。

谢谢

原来我的问题不在于 base64 编码,而在于我试图将其附加到的字符串。我必须执行以下操作,以便 python 不再将其视为字节编码。

base64.b64encode(output.read()).decode()