title: Using Python Flask, display images on webpage from mysql database
body: <p>I have created web based app using python flask for collecting students info.</p>
  
  <p>I have created a 'students' database in which I am collecting students profile including their photo by creating 'images' table
  For 'photo' filed, I am using BLOB data type in mysql.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>import mysql.connector
  import sys
  from PIL import Image
  import base64
  import six
  import io
  import PIL.Image
  import pymysql
  
  from flask import Flask,render_template,request,flash
  app=Flask(__name__)
  app.secret_key = 'dont tell anyone'
  
  
  @app.route('/')
  def display_image():
      db = mysql.connector.connect(user='root', password='######',
                                    host='localhost',
                                    database='students')
      cursor=db.cursor()
      sql1='select photo from images'
      cursor.execute(sql1)
      #db.commit()
      data=cursor.fetchall()
      #print type(data[0][0])
      file_like=io.BytesIO(data[0][0])
      img=PIL.Image.open(file_like)
      db.close()
      return render_template("home.html",data=file_like)
  
  if __name__=="__main__":
      app.run(debug=True)
  </code></pre>
  
  <p>I want to render the image to html file and want to display on web page
    Here is my html code:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
    &lt;meta http-equiv="X-UA-Compatible" content="ie=edge"&gt;
    &lt;title&gt;Document&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;h2&gt;This is my file upload page&lt;/h2&gt;
    &lt;table&gt;
      {% for item in data %}
      &lt;tr&gt;
        &lt;td&gt;{{item[0]}}&lt;/td&gt;
      &lt;/tr&gt;
      {% endfor %}
    &lt;/table&gt;  
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The result on the webpage is as follows:</p>
  
  <p><img src="https://i.stack.imgur.com/zV9ak.png" alt="enter image description here"></p>
  
  <p>I want to display image itself on the webpage, but it is displaying in binary mode. </p>
  
  <p>Can you pls brief me to fix this issue or suggest any article that help me to retrieve images from mysql using python flask and display on web page or can you provide me the code if possible.</p>
  
  <p>Thanking you
  With regards,
  Ramesh V</p>