图片转base64数据:
import base64
with open(r'c:\users\administrator\desktop\wh.jpg', 'rb') as f:
b64str = base64.b64encode(f.read()) #将图片编码成base64数据
with open(r'c:\users\administrator\desktop\img.txt', 'w+') as f:
f.write(b64str.decode()) #用decode()将bytes转换成str
base64数据转图片:
import base64
with open(r'c:\users\administrator\desktop\img.txt', 'r') as f:
data = base64.b64decode(f.read())
with open(r'c:\users\administrator\desktop\base64.jpg', 'wb') as f:
f.write(data)