【发布时间】:2018-05-31 04:38:10
【问题描述】:
我正在尝试使用以下代码将 PNG 图像编码为 Base64:
for files in os.listdir("."):
if files.endswith(".png"):
pngFile = open(files, 'rb')
base64data = pngFile.read().encode('base64').replace('\n','')
base64String = '<image xlink:href="data:image/png;base64,{0}" width="240" height="240" x="0" y="0" />'.format(base64data)
但是当我使用它时,它会报错:
AttributeError: 'bytes' object has no attribute 'encode'
我已经尝试了很多这样的解决方案: AttributeError: 'bytes' object has no attribute 'encode'; base64 encode a pdf file 但它只会引发另一个错误。顺便说一句,我正在使用 python 3
【问题讨论】:
标签: python python-3.x encoding base64 attributeerror