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

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

我创建了一个包含候选名称和图像的表,我在表中插入了值。我的问题是如何从数据库中检索存储的图像并在网页中查看?这有可能检索所有存储的图像吗?任何帮助都将非常感谢谢谢。

这是我的py

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从数据库中检索选定的图像

retrieve.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>
python mysql flask
1个回答
3
投票

是..可以检索存储在数据库中的所有图像。您可以使用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%;">
© www.soinside.com 2019 - 2024. All rights reserved.