I created a table with name and image of the candidate,I inserted the values in the table. My question is how can I retrieve the stored image from the database and view in webpage? Is that possible to retrieve all the stored images?Any help with this would be much grateful Thank you.
Here is my 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>
and here is my another HTML where I am trying to retrieve the selected image from the database using 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>
Yes.. It is possible to retrieve all images which is stored in database.You can Retrieve the images using 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)
In html, You can View the image by saving it in static folder and pass the image name to html.. here image_name is the value passed from flask to html
<img id="User_image" src="{{ url_for('static',filename=image_name) }}" style="width:100px;border-radius:50%;">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With