PDA

View Full Version : python "fast" filename



giuspen
November 25th, 2009, 09:59 PM
hi,
I need to serialize a binary content going through a function that writes a file name:

pixbuf.save("image_temp.png", "png")
fd = open("image_temp.png", "rb")
image_string = base64.b64encode(fd.read())
fd.close()
Does anybody know if it is possible somehow to skip the writing to disk?
The function pixbuf.save(...) described here (http://www.pygtk.org/docs/pygtk/class-gdkpixbuf.html#method-gdkpixbuf--save) is the only one that allows me to obtain the binary data I need and takes a filename, but is there a kind of cached, fast way without going through the writing to disk or that's the only way?
thanks in advance.

Paul Miller
November 25th, 2009, 10:22 PM
Sure. Save the data to a cStringIO object rather than a file.

giuspen
November 25th, 2009, 10:42 PM
Sure. Save the data to a cStringIO object rather than a file.

but the function

pixbuf.save("image_temp.png", "png")
requires a filename as first parameter, not a file object (like fd).
can you please modify my code introducing the StringIO as I don't understand how to implement it.
thanks a lot.

Can+~
November 26th, 2009, 01:25 AM
The following method on that documentation does what you want: gtk.gdk.Pixbuf.save_to_callback.

giuspen
November 26th, 2009, 10:39 AM
The following method on that documentation does what you want: gtk.gdk.Pixbuf.save_to_callback.

I tried it but after encoding it to base64 I had only few letters out of a very big image... and I can't understand what's wrong :(