Join the Stack Overflow Community
Stack Overflow is a community of 6.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

Is it possible to generate a functional image tag in html from a BytesIO buffer? I'd like to do something along these lines:

import matplotlib
matplotlib.use('Agg') 
import pylab
import Image
import io

temp_data = {'x':[1,2,3],'y':[2,4,5]}
pylab.plot(temp_data['x'], temp_data['y'])

img_buffer = io.BytesIO()
pylab.savefig(img_buffer, format = 'png')
img_buffer.seek(0)

img_tag = "<img src='data:image/png;base64,'" + img_buffer.getvalue() + "</img>"

May be necessary to re-format the value of the buffer in some way, or to change the content of the 'src' data. Thank you.

share|improve this question
up vote 2 down vote accepted

Problem solved:

towards the end of the code above, do this:

import base64
img_tag = "<img src='data:image/png;base64," + base64.b64encode(img_buffer.getvalue()) + "'/>"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.