場景
- 需要將圖片存入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
完事