场景
- 需要将图片存入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
完事