Note|Python 图片RGB矩阵和二进制string互转

def cvimage_to_base64(img):

    img_b = np.array(cv2.imencode(".jpg", img)[1]).tostring()

    img_b = base64.b64encode(img_b)

    img_b = img_b.decode("utf-8")

    return img_b

def base64_to_cvimage(image_string):

    img_bytes =bytearray(base64.b64decode(image_string))

    image_array = np.asarray(img_bytes, np.uint8)

    img = cv2.imdecode(image_array, cv2.IMREAD_COLOR)

    return img

你可能感兴趣的:(Note|Python 图片RGB矩阵和二进制string互转)