img_buffer_convert.py 579 字节
Newer Older
F
feilong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
# -*- coding: utf-8 -*-
import numpy as np
import cv2
import base64


def img_to_base64(img):
    return base64.b64encode(cv2.imencode('.jpg', img)[1]).decode()


def img_from_base64(img_base64):
    jpg_original = base64.b64decode(img_base64)
    jpg_as_np = np.frombuffer(jpg_original, dtype=np.uint8)
    img = cv2.imdecode(jpg_as_np, flags=1)
    return img


if __name__ == '__main__':
    img = cv2.imread('bug.jpg')

    img_base64 = img_to_base64(img)
    img = img_from_base64(img_base64)

    cv2.imshow('img_decode', img)
    cv2.waitKey()
    cv2.destroyAllWindows()