import base64 if __name__ == " __main__ " : dir = ' image.jpg '
basef =open(dir.split( ' . ' )[0]+ ' _base64.txt ' , ' w ' ) with open (dir, ' rb ' ) as f: base64_data = base64.b64encode(f.read()) s = base64_data.decode() data = ' data:image/jpeg;base64,%s ' % s # print(data) basef.write(data) basef.close()
最后,会在当前目录下生成对应的base64.txt文件,同理,解码用
base64.b64decode()