【发布时间】:2022-04-29 05:03:30
【问题描述】:
我使用 python 烧瓶创建了基于 Web 的应用程序来收集学生信息。
我创建了一个“学生”数据库,通过创建“图像”表来收集学生个人资料,包括他们的照片 对于“照片”文件,我在 mysql 中使用 BLOB 数据类型。
这是我的代码:
import mysql.connector
import sys
from PIL import Image
import base64
import six
import io
import PIL.Image
import pymysql
from flask import Flask,render_template,request,flash
app=Flask(__name__)
app.secret_key = 'dont tell anyone'
@app.route('/')
def display_image():
db = mysql.connector.connect(user='root', password='######',
host='localhost',
database='students')
cursor=db.cursor()
sql1='select photo from images'
cursor.execute(sql1)
#db.commit()
data=cursor.fetchall()
#print type(data[0][0])
file_like=io.BytesIO(data[0][0])
img=PIL.Image.open(file_like)
db.close()
return render_template("home.html",data=file_like)
if __name__=="__main__":
app.run(debug=True)
我想将图像渲染为 html 文件并想在网页上显示 这是我的html代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h2>This is my file upload page</h2>
<table>
{% for item in data %}
<tr>
<td>{{item[0]}}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
网页上的结果如下:
我想在网页上显示图像本身,但它以二进制模式显示。
您能否简要介绍我解决此问题或建议任何文章,以帮助我使用 python 烧瓶从 mysql 检索图像并显示在网页上,或者如果可能的话,您能否提供代码。
谢谢你 带着敬意, 拉梅什V
【问题讨论】:
-
感谢您的反馈
标签: python