方法1
编码
import base64
from io import BytesIO
from PIL import Image
def loadimg(imgpath):
"""
加载图片
"""
return Image.open(imgpath)
def image_to_base64(imgpath):
"""
将打开后的图片转换为base64格式输出
"""
image = loadimg(imgpath)
buff = BytesIO()
image.save(buff,format="JPEG")
imagedata = base64.b64encode(buff.getvalue())
imgbase64= imagedata.decode()
return imgbase64
解码
def base64_to_image(imagebase64):
"""
输入base64,输出图片
"""