python 圖像base64編碼

場景

  1. 需要將圖片存入redis,redis接受的數據是str,所以要將圖片轉爲base64

代碼示例

import base64
from PIL import Image
improt io

def convert_image_to_b64(image_path):
	image = Image.open(image_path)
	img_bytes = io.BytesIO()
    image.save(img_bytes, format="JPEG")
    bytes = img_bytes.getvalue()
    b64 = base64.b64encode(bytes).decode('utf-8')
    return b64

完事

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章