import cv2 import numpy as np import base64 mat = cv2.imread("/home/lab/2.png") # Mat to Base64 string = base64.b64encode(cv2.imencode('.png', mat)[1]).decode() print(string) # Base64 to Mat img_original = base64.b64decode(string) img_np = np.frombuffer(img_original, dtype=np.uint8) img = cv2.imdecode(img_np, cv2.IMREAD_UNCHANGED) cv2.imwrite('/home/lab/1.png',img)