Storing matplotlib figure's image data straightway to SQL Server

Hello matplotlib users!

I 'm using matplotlib for one week and I 'm excited.
The only thing I couldn’t do is to save image’s contect straightway to SQL server. So I 'm searching for a function that store figure’s image as binary data in a python variable in order to put the content of this variable inside an postgresql query.

For example:

matplotlib code

conn = psycopg2.connect(…)
curs = conn.cursor()
image_data=###The matplotlib function I’m searching###
sql = “update med_graphs set image=%s;”
curs.execute(sql,image_data,))
conn.commit()

I found only the figure.savefig() function that saves figure’s image only as a filename in the filesystem and not in a local variable.

Any help would very much appreciated!
Panagiotis

http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.savefig

savefig can take any file-like object, not just a file name.

Check your matplotlib version, and upgrade if you can.

Regards,

-JJ

···

On Fri, Dec 18, 2009 at 3:06 AM, Panagiotis Kontaxis <pkontaxi@...287...> wrote:

I found only the figure.savefig() function that saves figure's image only as
a filename in the filesystem and not in a local variable.

Thank you very much for your answer!
I post the full solution to anyone else maybe need it:
try:
conn = psycopg2.connect(“dbname=’…’ user=’…’ host=‘127.0.0.1’ password=’…’”);
except:
print “I am unable to connect to the database”
curs = conn.cursor()
… code to create image/graph… #canvas contains graph
import StringIO
data=StringIO.StringIO()
canvas.print_figure(data, dpi=150, format=“png”)
sql = “update table set image=%s;”
curs.execute(sql,(encoded_data,))
conn.commit()

2009/12/19 Jae-Joon Lee <lee.j.joon@…287…>

···

On Fri, Dec 18, 2009 at 3:06 AM, Panagiotis Kontaxis <pkontaxi@…287…> wrote:

I found only the figure.savefig() function that saves figure’s image only as

a filename in the filesystem and not in a local variable.

http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.savefig

savefig can take any file-like object, not just a file name.

Check your matplotlib version, and upgrade if you can.

Regards,

-JJ