Skip to main content
 首页 » 编程设计

python之在 Flask 中显示图像

2025年01月19日9cloudgamer

我正在尝试使用此库从我的 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)<br/> return render_template('display.html', image = image)


并将此变量放入 html 中:

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

但是我无法显示图像(我使用的是 python3 和 google chorme)有人可以帮忙获取吗?

请您参考如下方法:

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