使用Python Flask在网页上显示MySQL数据库中的图像,可以按照以下步骤进行:
from flask import Flask, render_template
import pymysql
# 连接到MySQL数据库
conn = pymysql.connect(host='localhost', user='username', password='password', db='database_name')
# 创建游标对象
cursor = conn.cursor()
# 执行查询语句
cursor.execute("SELECT image_data FROM images_table")
# 获取查询结果
images = cursor.fetchall()
# 关闭游标和数据库连接
cursor.close()
conn.close()
# 创建Flask应用程序
app = Flask(__name__)
# 定义路由,用于显示图像数据
@app.route('/')
def display_images():
return render_template('index.html', images=images)
<!DOCTYPE html>
<html>
<head>
<title>Display Images</title>
</head>
<body>
{% for image in images %}
<img src="data:image/jpeg;base64,{{ image[0] }}" alt="Image">
{% endfor %}
</body>
</html>
这是一个基本的示例,你可以根据实际需求进行修改和扩展。在实际应用中,你可能还需要处理图像的上传、存储和其他相关操作。
领取专属 10元无门槛券
手把手带您无忧上云