sximage.py 549 B

123456789101112131415
  1. from base64 import b64decode, b64encode
  2. import numpy as np
  3. import cv2
  4. def base64_to_np(img_data):
  5. img_data = img_data.split(',', 1)[-1]
  6. # b64decode -> base64图片解码
  7. # np.fromstring -> 从字符串中的文本数据初始化的新一维数组
  8. # numpy.fromstring(string, dtype=float, count=-1, sep='')
  9. return cv2.imdecode(np.fromstring(b64decode(img_data), dtype=np.uint8), cv2.IMREAD_COLOR)
  10. def np_to_base64(np_data):
  11. img_str = cv2.imencode('.jpg', np_data)[1].tostring()
  12. return b64encode(img_str).decode('utf-8')