【发布时间】:2018-09-03 06:14:12
【问题描述】:
我试图在转换为 base64 后显示一个 PIL 对象。 我在 src 标记中获得了 base64 值,但即使在解码后也没有呈现响应
import base64
import io
def newrules(request):
pic = con(select.fname)
print(pic)
buffered = io.BytesIO()
pic.save(buffered, "PNG")
img_str = base64.b64encode(buffered.getvalue())
template_code = """
{% load static %}
<!DOCTYPE HTML>
<html>
<body>
{% block pagecontent %}
<div>
<img src="data:image/png;base64,{{ img_str }}">
</div>
<div>
{{ img_str }}
</div>
</body>
{% endblock %}
</html>
"""
template = engines['django'].from_string(template_code)
return HttpResponse(template.render(context={'img_str': img_str}))
我们将不胜感激。
【问题讨论】:
-
在副本中查看我的 Python3 答案 - 基本上你需要通过
img_str = base64.b64encode(buffered.getvalue().decode()将str而不是bytes传递给模板引擎。
标签: html django python-3.x base64 python-imaging-library