gpt4 book ai didi

python - 如何使用 Flask 从 MySQL 数据库中检索保存为 BLOB 类型的选定图像

转载 作者:可可西里 更新时间:2023-11-01 07:06:51 24 4
gpt4 key购买 nike

我创建了一个包含候选人姓名和图像的表格,我将值插入表格中。我的问题是如何从数据库中检索存储的图像并在网页中查看?是否可以检索所有存储的图像?如果对此有任何帮助,我们将不胜感激谢谢。

这是我的代码

import os
from flask import Flask,request,render_template
from flaskext.mysql import MySQL
mysql=MySQL()

app=Flask(__name__)
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = 'root'
app.config['MYSQL_DATABASE_DB'] = 'matrimony'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)
APP_ROOT=os.path.dirname(os.path.abspath(__file__))


@app.route('/',methods=['GET','POST'])
def get_data():
if request.method=='POST':
print("hlo")
candidate_name=request.form['cname']
file=request.files['canpic']

conn = mysql.connect()



target = os.path.join(APP_ROOT, 'file')
if not os.path.isdir(target):
os.makedirs(target)
print (target)
filename = file.filename
destination = "/".join([target, filename])
print (destination)
file.save(destination)
print "saved"
datafile = open(destination, "rb")
print "file opend"
d = open("F:/five pro/testvote/test.jpg", "wb")
print "file test opnend"
thedata = datafile.read()
print "file readed"
d.write(thedata)
d.close()
datafile.close()

b1 = open("F:/five pro/testvote/test.jpg", 'rb').read()
target1 = os.path.join(APP_ROOT, 'file')


conn = mysql.connect()
cursor = conn.cursor()

print ("yoyo")
cursor.execute("insert into Votetable2 values (%s,%s)", (candidate_name,b1))

print "execuet"

conn.commit()
return render_template('final_pic.html')

if __name__=='__main__':
app.run(debug=True)

final_pic.html:

<!DOCTYPE html>
<html>
<body>

<form method="POST" action="/" enctype="multipart/form-data">

Candidate Name: <input type="text" name="cname"><br>
Candidate Image<input type="file" name="canpic" accept="image/*"><br>



<input type="submit" value="Submit">

</form>

</body>
</html>

这是我的另一个 HTML,我在其中尝试使用 select candidate_name, candidate_image from votetable2

从数据库中检索所选图像

检索.html

<!DOCTYPE html>
<html lang="en">


<body><form>

<table border="2"><tr>
<td>CANDIDATE NAME</td>
<td>CANDIDATE IMAGE</td>
</tr>

{% for row in a %}

<tr>
<td>{{ row[0] }}<input type="submit" value="Done"></td>
<td>{{ row[1] }}

</tr>
{% endfor %}
</table>
</form>
</body></html>

最佳答案

是的。可以检索存储在数据库中的所有图像。您可以使用 IO 检索图像。

cursor.execute("select picture,voterid from registration")
data = cursor.fetchall()
for row in dataw:
file_like = io.BytesIO(row[0])
file = PIL.Image.open(file_like)
target = os.path.join("/path-to-save/", 'folder-save')
if not os.path.isdir(target):
os.makedirs(target)
destination = "/".join([target, file.filename])
file.save(destination)

在html中,您可以通过将图像保存在静态文件夹中并将图像名称传递给html来查看图像..这里的image_name是从flask传递给html的值

<img id="User_image" src="{{ url_for('static',filename=image_name) }}" style="width:100px;border-radius:50%;">

关于python - 如何使用 Flask 从 MySQL 数据库中检索保存为 BLOB 类型的选定图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47219947/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com