导航

pyhton显示redis base64编码图片

Posted on 2023-12-05 16:41  听雨轩!  阅读(31)  评论(0编辑  收藏  举报

1、直接显示已存储的图片:

from PIL import Image

 im = Image.open(r'e:\p1.jpeg' )

 im.show()
2、python存储redis图片:
db = redis.Redis(host='127.0.0.1', port= 6379, password= '********', db= 0,decode_responses=True,charset='UTF-8',encoding='UTF-8')

with open(r'e:\p1.jpeg','rb') as f:
    base64_data = base64.b64encode(f.read())
    # print(base64_data)
    db.set('user_img',base64_data)
 
3 pyhton获取redis图片:
img_base64 = db.get('user_img')
data = base64.b64decode(img_base64)
4 显示图片:在软件内显示
import redis  
import io
import base64  
from PIL import Image  
import matplotlib.pyplot as plt  
image = Image.open(io.BytesIO(data))  
plt.imshow(image)  
plt.show()

 

5 也可以这样显示:在IE上显示
from PIL import Image
image = Image.open(io.BytesIO(data))  
image.show()