numpy 圖片矩陣 轉換成 base64

import base64
import cv2
from PIL import Image
from io import BytesIO

def image():

    img_file = 'demo.jpg'
    img = cv2.imread(img_file)
    image = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    output_buffer = BytesIO()
    image.save(output_buffer, format="JPEG")
    byte_data = output_buffer.getvalue()
    base64_str = base64.b64encode(byte_data)
    print(str(base64_str, encoding = "utf-8"))
相關文章
相關標籤/搜索