我正在尝试使用以下代码将 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 对 pdf 文件进行编码,但它只会引发另一个错误。顺便说一句,我正在使用 python 3