title: How To Display Image to HTML using python
body: <p><em>To display image from the path specified to the HTML browser in python. I have coded in this manner.</em></p>
  
  <p>index.html</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
     &lt;form enctype = "multipart/form-data" 
                       action = "save_file.py" method = "post"&gt;
     &lt;p&gt;File: &lt;input type = "file" name = "filename" /&gt;&lt;/p&gt;
     &lt;p&gt;&lt;input type = "submit" value = "Upload" /&gt;&lt;/p&gt;
     &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>save_file.py</strong></p>
  
  <pre><code>#!C:/Users/Vitriv-Desktop/AppData/Local/Programs/Python/Python36-32/python.exe
  
  import cgi, os
  import cgitb; cgitb.enable()
  from PIL import Image
  
  form = cgi.FieldStorage()
  
  # Get filename here.
  fileitem = form['filename']
  
  # Test if the file was uploaded
  if fileitem.filename:
     # strip leading path from file name to avoid 
     # directory traversal attacks
     fn = os.path.basename(fileitem.filename)
     open('C:/Apache24/htdocs/tmp/' + fn, 'wb').write(fileitem.file.read())
  
     message = 'The file "' + fn + '" was uploaded successfully'
     path = 'C:/Apache24/htdocs/tmp/' + fn
     image = Image.open('C:/Apache24/htdocs/tmp/' + fn)
     image.show()
  else:
     message = 'No file was uploaded'
     #Content-Type: text/html\n
  print ("""\
  Content-Type: image/jpg\n
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;body&gt;
     &lt;p&gt;%s &lt;/p&gt;
  &lt;img src="%s" alt="C:/Apache24/htdocs/tmp/%s"&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  """ % (message,path,fn,))
  </code></pre>
  
  <p><br/>
  <strong>Expected Output:</strong> It should display image taking from that path specified. <br/><br/>
  <strong>Actual Output:</strong> Display with img block with text C:/Apache24/htdocs/tmp/xy.jpg</p>