Source code for blocks.utils.base64
import cv2
import base64
import numpy as np
__all__ = ['base64_to_cv2', 'cv2_to_base64', 'bytes_to_base64']
[docs]def base64_to_cv2(string_base64: str, imread_flag: int=cv2.IMREAD_COLOR) -> np.ndarray:
"""Transform base64-encoded string to OpenCV image."""
decoded_bytes = base64.b64decode(string_base64.encode())
decoded_np = np.fromstring(decoded_bytes, np.uint8)
return cv2.imdecode(decoded_np, imread_flag)
[docs]def cv2_to_base64(image: np.ndarray, extension: str='.png') -> str:
"""Transform OpenCV image to base64-encoded string."""
return base64.b64encode(cv2.imencode(extension, image)[1]).decode('utf-8')
[docs]def bytes_to_base64(data: bytes) -> str:
"""Transform bytes to base64-encoded string."""
return base64.b64encode(data).decode('utf-8')