import cStringIO my_stringIObytes = cStringIO.StringIO() plt.savefig(my_stringIObytes, format='jpg') my_stringIObytes.seek(0) my_base64_jpgData = base64.b64encode(my_stringIObytes.read())