Python to achieve opencv used by the image format and Base64 conversion

Time:2021-5-4

The NP image (imread image) is transcoded to Base64 format


def image_to_base64(image_np):
 
 image = cv2.imencode('.jpg',image_np)[1]
 image_code = str(base64.b64encode(image))[2:-1]
 
 return image_code

Parsing Base64 encoding into opencv available pictures

def base64_to_image(base64_code):
 
 #Base64 decoding
 img_data = base64.b64decode(base64_code)
 #Convert to NP array
 img_array = np.fromstring(img_data, np.uint8)
 #Convert to opencv format
 img = cv2.imdecode(img_array, cv2.COLOR_RGB2BGR)
 
 return img

The above image format and Base64 conversion used by Python to realize opencv is all the content shared by Xiaobian. I hope it can give you a reference, and I hope you can support developer more.