base64转图片
from django.test import TestCase
import base64
import time
import os
def base64_to_image(base64Img):
# base64转image
image_data = base64.b64decode(base64Img)
image_url = "2.jpg"
with open(image_url, 'wb') as f:
f.write(image_data) # 打开路径将结果写入到文件中
return image_url
def image_to_base64(file_path):
# image转base64
with open(file_path, "rb") as f: # 转为二进制格式
base64_data = base64.b64encode(f.read()) # 使用base64进行加密
file = open('1.txt', 'wt') # 写成文本格式
file.write(str(base64_data, encoding="utf-8"))
file.close()
if __name__ == '__main__':
with open("1.txt", "rb") as f:
base64_to_image(f.read())