在Flask中显示图像

问题描述 投票:2回答:1

我试图通过使用此库从我的sqlite3数据库渲染图像:

#!/usr/bin/python3 from flask import Flask, render_template, request, send_file from flask_sqlalchemy import SQLAlchemy from io import BytesIO import base64 @app.route('/show/') def show(): file_data = FileContents.query.filter_by(id=3).first() image = b64encode(file_data.data)
return render_template('display.html', image = image)


并将此变量放在html中:

<html>
<body>
<img src="data:image;base64,{{image}}"/>
</body>
</html>

但是我无法显示图像(我使用的是python 3和谷​​歌浏览器)有人可以帮忙搞定吗?

python flask
1个回答
1
投票

您可以添加扩展名。示例:<img src="data:image/png;base64,{{ image }}"\>并将图像解码为utf-8 return render_template('display.html', image = image.decode('utf8'))

© www.soinside.com 2019 - 2024. All rights reserved.