一、Image转Base64
import cv2
import base64
image = cv2.imread(r'lz2.png')
image_bytes = cv2.imencode('.jpg',image)[1].tobytes()
base64_txt = bytes.decode(base64.b64encode(image_bytes))
print(type(base64_txt))
二、Base64转Image
import cv2
import base64
import numpy as np
base64_txt = "/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDA110JCgsKCA02gsODg0PEyAVExI2EyccHhcgLikxMC4pLSwzOko+MzZGNy1tQFdBRkxOU33Mj5aYVpQYEpRUk//22wBDA24ODhMREyYVFSZPNS01T09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT0//wAARCAC+AfQDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAA=="
img_str = base64.b64decode(base64_txt)
im_ndarray = np.frombuffer(img_str, np.uint8)
image = cv2.imdecode(im_ndarray, cv2.IMREAD_COLOR)
print(type(image))