sximage.py 521 B

1234567891011121314151617
  1. import base64
  2. from base64 import b64decode
  3. import numpy as np
  4. import cv2
  5. # base64格式转numpy格式
  6. def base64_to_np(img_data):
  7. color_image_flag = 1
  8. img_data = img_data.split(',',1)[-1]
  9. return cv2.imdecode(np.fromstring(b64decode(img_data), dtype=np.uint8), color_image_flag)
  10. def base64_cv2(base64_str):
  11. base64_str = base64_str.split(',', 1)[-1]
  12. img_str = base64.b64decode(base64_str)
  13. np_arr = np.fromstring(img_str, np.int8)
  14. image = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
  15. return image