Hi,
I'm writing a fractal-generating program in a mixture of C and
Python. Python handles all the GUI parts using PyGTK. After finishing
the calculations, I have a buffer containing the RGB data of the
image. I can display this fine on screen using draw_rgb_image. Now I
need to save the image data into a file. gtk.gdk.Pixbuf.save() would
be ideal, but I'm stumped trying to create a Pixbuf from my image
data. In C, gdk_pixbuf_new_from_data would be ideal, but I can't find
the PyGTK equivalent. There's new_from_xpm_data() and new_from_inline(),
but those seem to require translating the image into a text format,
which seems slow and kind of backwards.
I'd prefer to avoid introducing a dependency on PIL or Numeric.
Any suggestions?
Thanks,
--
Edwin 6 10106
> I'm writing a fractal-generating program in a mixture of C and Python. Python handles all the GUI parts using PyGTK. After finishing the calculations, I have a buffer containing the RGB data of the
[...]
Here is something I've used in an application some time ago. Not
sure if this is what you want, but will give you a hint.
loader = gtk.gdk.PixbufLoader("jpeg")
loader.write(data)
loader.close()
pixbuf = loader.get_pixbuf()
--
Gustavo Niemeyer http://niemeyer.net
>>>>> "Edwin" == Edwin Young <ed***@localhost.localdomain> writes:
Edwin> Hi,
Edwin> I'm writing a fractal-generating program in a mixture of C
Edwin> and Python. Python handles all the GUI parts using
Edwin> PyGTK. After finishing the calculations, I have a buffer
Edwin> containing the RGB data of the image. I can display this
Edwin> fine on screen using draw_rgb_image. Now I need to save the
Edwin> image data into a file. gtk.gdk.Pixbuf.save()
gtk.gdk.Pixbuf does have a save method http://www.gnome.org/~james/pygtk-do...gdkpixbuf.html
Alternatively, if you have a pixbuf instance that contains your image
data, you can access the data as an RGBA array with
# pb is a gtk.gdk.Pixbuf
pa = pb.get_pixels_array() #pygtk 2.2 and
or
pa = pb.pixel_array # pygtk-2.0
You can then access the rgba components, eg
pa[:,:,0] # red
pa[:,:,1] # green
pa[:,:,2] # blue
pa[:,:,3] # alpha
and write them to a file anyway you want.
Or am I not understanding you?
JDH
John Hunter <jd******@ace.bsd.uchicago.edu> writes: >> "Edwin" == Edwin Young <ed***@localhost.localdomain> writes:
Edwin> Hi,
Edwin> I'm writing a fractal-generating program in a mixture of C Edwin> and Python. Python handles all the GUI parts using Edwin> PyGTK. After finishing the calculations, I have a buffer Edwin> containing the RGB data of the image. I can display this Edwin> fine on screen using draw_rgb_image. Now I need to save the Edwin> image data into a file. gtk.gdk.Pixbuf.save()
gtk.gdk.Pixbuf does have a save method
Right, the problem is getting the data *into* the pixbuf in the first
place. Basically I have the image in a long string in memory and need
to get that into the Pixbuf somehow. The pixel_array is read-only, so
I can't use that.
I don't think Gustav's idea will work because the loader expects the
*encoded* pixels (+ header, etc) from a jpeg file, and if I already
had a jpeg encoder I could just save the file myself.
Thanks,
--
Edwin
>>>>> "Edwin" == Edwin Young <ed***@localhost.localdomain> writes:
Edwin> Right, the problem is getting the data *into* the pixbuf in
Edwin> the first place. Basically I have the image in a long
Edwin> string in memory and need to get that into the Pixbuf
Edwin> somehow. The pixel_array is read-only, so I can't use that.
In matplotlib, I have an image module that renders to a
unsigned char * buffer in C, and provides an as_str method
to access that buffer from python.
char Image_as_str__doc__[] =
"numrows, numcols, s = as_str()"
"\n"
"Call this function after resize to get the data as string"
;
static PyObject *
Image_as_str(ImageObject *image, PyObject* args) {
if (!PyArg_ParseTuple(args, ":as_str"))
return NULL;
return Py_BuildValue("lls#", image->rowsOut, image->colsOut,
image->bufferOut, image->colsOut*image->rowsOut*4);
}
On the pygtk side, I load this into a Pixbuf pixel array as follows
(in this case the image data is RGBA so the array is M x N x 4)
rows, cols, s = im.as_str()
X = fromstring(s, UInt8)
X.shape = cols, rows, 4
pb=gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,
has_alpha=1, bits_per_sample=8,
width=rows, height=cols)
try: pa = pb.get_pixels_array()
except AttributeError: pa = pb.pixel_array
pa[:,:,:] = X
gc = self.new_gc()
pb.render_to_drawable(self.gdkDrawable, gc.gdkGC, 0, 0,
int(x), int(self.height-y), rows, cols,
gdk.RGB_DITHER_NONE, 0, 0)
This actually works quite well, performance wise. At least on the
systems I've tested. You'll need pygtk compiled with numeric support.
In an earlier thread, Cedric just provided a link to a win32 installer
with Numeric if you need it.
JDH >> "Edwin" == Edwin Young <ed***@localhost.localdomain> writes: Edwin> Right, the problem is getting the data *into* the pixbuf in Edwin> the first place. Basically I have the image in a long Edwin> string in memory and need to get that into the Pixbuf Edwin> somehow. The pixel_array is read-only, so I can't use that.
John Hunter <jd******@ace.bsd.uchicago.edu> writes: On the pygtk side, I load this into a Pixbuf pixel array as follows (in this case the image data is RGBA so the array is M x N x 4)
rows, cols, s = im.as_str() X = fromstring(s, UInt8) X.shape = cols, rows, 4 pb=gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, has_alpha=1, bits_per_sample=8, width=rows, height=cols) try: pa = pb.get_pixels_array() except AttributeError: pa = pb.pixel_array
pa[:,:,:] = X
gc = self.new_gc() pb.render_to_drawable(self.gdkDrawable, gc.gdkGC, 0, 0, int(x), int(self.height-y), rows, cols, gdk.RGB_DITHER_NONE, 0, 0)
This actually works quite well, performance wise. At least on the systems I've tested. You'll need pygtk compiled with numeric support. In an earlier thread, Cedric just provided a link to a win32 installer with Numeric if you need it.
That looks fine, but ideally I want to avoid having complicated
dependencies- I don't want my program to be one of the ones that
require you to download ten others just to run them. Pygtk itself
seems to be pretty widely distributed in popular distributions, but
PyGTK-with-Numeric isn't.
I think I'll just write a tiny C extension to copy the data into
the pixbuf.
Regards,
--
Edwin
>>>>> "Edwin" == Edwin Young <ed***@localhost.localdomain> writes:
Edwin> That looks fine, but ideally I want to avoid having
Edwin> complicated dependencies- I don't want my program to be one
Edwin> of the ones that require you to download ten others just to
Edwin> run them. Pygtk itself seems to be pretty widely
Edwin> distributed in popular distributions, but
Edwin> PyGTK-with-Numeric isn't.
I brought this up with Cedric Gustin, the pygtk win32 installer
distributer, because I need Numeric support for matplotlib, and he
immediately built and uploaded pygtk for windows with Numeric (and
will do likewise for future builds). http://www.async.com.br/faq/pygtk/in...=faq21.012.htp
JDH This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Samuele Giovanni Tonon |
last post by:
hi,
i'm trying to develop a trivial application which random copy files
from a directory to another one.
i made it using pygtk for the graphical interface, however i find
some problem with...
|
by: Johan |
last post by:
Hello!
I'm trying to figure out how to use PYGTK to implement a rudimentary UI:
I want to have an Image as the background, and then be able to put buttons
(eventually icons, but buttons for...
|
by: dataangel |
last post by:
I want to make a pygtk app that consists completely of a window. When I
run it, a menu should appear where the mouse cursor is. I've been
looking at the official pygtk tutorial and documentation,...
|
by: Jeremy Bowers |
last post by:
I have an image in the Python Image Library. I'm trying to get it into
PyGTK in color. Is there any way to do this cross-platform, preferably
without writing to anything to the disk?
PIL...
|
by: manatlan |
last post by:
Hello, I'm new to pygtk (but i know wxpython very well)
I'm trying to play with this example :
http://www.pygtk.org/pygtk2tutorial/sec-CellRenderers.html#filelistingfig
(the code is here
:...
|
by: TPJ |
last post by:
GUI's etc: PyGtk on Windows
"(...) So if someone develops mainly for X and just wants to make sure
that it is not impossible to run on Windows, you can use PyGTK. (...)",
July 2nd, 1999
pyGTK...
|
by: xrado |
last post by:
when i say window.hide(), window dont hide imidetly
i want to hide it for a few seconds, do something and then show it back
how can i do this?
i have this example:
import pygtk,time...
|
by: stevemcc |
last post by:
I am trying to make a game using pygtk. It requires that there be an image in the background and widgets that can go in front of the image. I have tried defining a background image for the Mainwindow...
|
by: Laharl |
last post by:
I have an SVG file that's stored as data in an associated XML file, and I need to display the SVG. However, since the SVG is loaded from the XML, it's currently in the program as a string. Is there...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |