embed image to HTML with python

0

I am developing a small website with Python and Flask. I came across a small conflict, the web is for a developer event that will be in the company and has a section of registration and another of projects, where all the projects that we already approved are shown, and in the registration part they are all the equipment data. The data that causes conflict is the image because when I upload it I do the following:

# leemos la imagen
    with open(picture, 'rb') as f:
        binary_data = f.read()

As the comment says, I read the image with python using that code and convert it to a binary file, so once I have the file in the variable binary_data I simply upload it to the database in a BLOB type of data. Now, when I want to show it on the web I do the following:

tags=[]
logo_sql = "select PROJECT_ID, LOGO from Engine1.PROJECT WHERE ACCEPTED = '1'"
stmt = ibm_db.exec_immediate(conn,logo_sql)

pictures = {}
row = ibm_db.fetch_assoc(stmt)
while(row):
    pictures[row['PROJECT_ID']] = row['LOGO'].encode('base64').replace('\n', '')
    img_tag = '<img src="data:image/png;base64,%s">' %pictures[row['PROJECT_ID']]

    tags.append(img_tag)

    row = ibm_db.fetch_assoc(stmt)


return render_template('site.html', catego=catego, tags = tags)

tags [] is a list that will contain the image tags, the query takes out the data type BLOB, which in this case is the image that I uploaded previously and by means of an example that I found on the internet to embed a direct HTML image from python, I create a label with the ID of each of the projects, and in the while I create as many as necessary. The problem is here, when I want to send that data in the render_template to HTML, htlm does not understand it and shows it to me in the following way:

and we see that he does not interpret it, he only shows it in plain text, someone who can help me to show the image and not the data?

    
asked by Roger 01.02.2018 в 21:42
source

1 answer

0

First of all, I do not know if you use Python 2 or 3. The encoding can be logically different depending on the version. I can help you with python 3. Thus, a possible solution would be first importing the base64 library:

import base64

Then, within while replace the first line with the following:

pictures[row['PROJECT_ID']] = base64.encodestring(row['LOGO'])

I hope it will help you.

    
answered by 07.02.2018 в 23:52