美文网首页
PIL image 转 base64

PIL image 转 base64

作者: 不如做一只猫 | 来源:发表于2018-08-31 10:18 被阅读11次

PIL image to base64

最近有个做验证码的需求,要求直接返回图片base64编码后的数据
验证码是通过PIL的iamge生成的,不想image.save()保存一遍再打开

# py3
import base64
from io import BytesIO

buffered = BytesIO()
image.save(buffered, format="PNG")
img_str = base64.b64encode(buffered.getvalue())

然后在html中把img_str替换掉就可以了。

<img src="data:image/png;base64, img_str"/>

相关文章

网友评论

      本文标题:PIL image 转 base64

      本文链接:https://www.haomeiwen.com/subject/xnfswftx.html