1.base64---->numpy
import numpy as np
import cv2
import base64
def base2numpy(base):
"""
@param base:浏览器可识别的base64编码格式
"""
img_bin= base64.b64decode(base.split(';base64,')[-1])
img_buff = np.frombuffer(img_bin, dtype='uint8')
image = cv2.imdecode(img_buff,1)
return image
2.numpy—>base64
import numpy as np
import cv2
import base64
def numpy2base(image):
"""
@param image:numpy数组格式图片
"""
success, encoded_image = cv2.imencode(".jpg", image)
byte_data = encoded_image.tobytes()
base = "data:image/jpg;base64," + base64.b64encode(byte_data).decode('utf-8')
return base