How to add image file in html using python

0 votes

How to add image file in HTML using python

Nov 1, 2020 in Python by jeeva
• 120 points
1,886 views

2 answers to this question.

0 votes

Hey, @Jeeva,

You can use this code to directly embed the image in your HTML: Python 3

import base64
data_uri = base64.b64encode(open('Graph.png', 'rb').read()).decode('utf-8')
img_tag = '<img src="data:image/png;base64,{0}">'.format(data_uri)
print(img_tag)

Python 2.7

data_uri = open('11.png', 'rb').read().encode('base64').replace('\n', '')
img_tag = '<img src="data:image/png;base64,{0}">'.format(data_uri)

print(img_tag)

Alternatively for Python <2.6:

data_uri = open('11.png', 'rb').read().encode('base64').replace('\n', '')
img_tag = '<img src="data:image/png;base64,%s">' % data_uri

print(img_tag)
answered Nov 1, 2020 by Gitika
• 65,930 points
0 votes
Hi,
I have implemented as below

from robot.api import logger

img = "example.jpg"
strImg = '"{}"'.format(img)

img_tag = "<img src=" + strImg + ">"
logger.info(img_tag, html=True)
answered Feb 8 by anonymous

Related Questions In Python

0 votes
1 answer

How to check latest change time of each file using glob in python?

You can get the changing time from ...READ MORE

answered Jan 2, 2019 in Python by Omkar
• 69,150 points
399 views
0 votes
2 answers

How can I write a program to add two numbers using functions in python?

there is sum() function as a built ...READ MORE

answered Oct 24, 2020 in Python by anonymous
16,310 views
+1 vote
1 answer

How to check if a string is in a file, using python?

Hi, Try the below given code: with open('myfile.txt') as ...READ MORE

answered Jul 4, 2019 in Python by Shabnam
• 930 points
629 views
0 votes
0 answers

How to handle large files using file handling in python?

I have used file handling for smaller ...READ MORE

Aug 2, 2019 in Python by Waseem
• 4,540 points
171 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,259 views
0 votes
0 answers
0 votes
1 answer

How to save an image locally using Python in which we already know the URL address?

Hi, @Roshni, The following code is used to ...READ MORE

answered Jun 26, 2020 in Python by Gitika
• 65,930 points
587 views
0 votes
4 answers

How to print objects of class using print function in Python?

>>> class Test: ... ...READ MORE

answered Dec 15, 2020 in Python by Roshni
• 10,480 points
53,792 views