Use the StringIO object as a file object that can be specified for the canvas print_png function.
from cStringIO import StringIO sio = StringIO() canvas.print_png(sio) return sio.getvalue()
(if you are using Python 3, use io.BytesIO instead of cStringIO )
source share