import base64
with open('./test.jpg', mode='rb') as file:
data = file.read()
print(base64.b64encode(data).decode())
基于Base64字符串还原成一个文件
import base64
source = '/9j/4AAQSkZJRgABAQEASABIAAD/2wBAf/2Q=='
#此处为文件内容编码后的base64代码
data = base64.b64decode(source)
with open('./test123.jpg', mode='wb') as file:
file.write(data)