473,714 Members | 2,395 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PyGTK: creating a pixbuf from image data


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_da ta() 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
Jul 18 '05 #1
6 10189
> 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.PixbufL oader("jpeg")
loader.write(da ta)
loader.close()
pixbuf = loader.get_pixb uf()

--
Gustavo Niemeyer
http://niemeyer.net

Jul 18 '05 #2
>>>>> "Edwin" == Edwin Young <ed***@localhos t.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_a rray() #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

Jul 18 '05 #3
John Hunter <jd******@ace.b sd.uchicago.edu > writes:
>> "Edwin" == Edwin Young <ed***@localhos t.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
Jul 18 '05 #4
>>>>> "Edwin" == Edwin Young <ed***@localhos t.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__d oc__[] =
"numrows, numcols, s = as_str()"
"\n"
"Call this function after resize to get the data as string"
;
static PyObject *
Image_as_str(Im ageObject *image, PyObject* args) {

if (!PyArg_ParseTu ple(args, ":as_str"))
return NULL;

return Py_BuildValue(" lls#", image->rowsOut, image->colsOut,
image->bufferOut, image->colsOut*imag e->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.Pixb uf(gtk.gdk.COLO RSPACE_RGB,
has_alpha=1, bits_per_sample =8,
width=rows, height=cols)
try: pa = pb.get_pixels_a rray()
except AttributeError: pa = pb.pixel_array

pa[:,:,:] = X

gc = self.new_gc()
pb.render_to_dr awable(self.gdk Drawable, 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

Jul 18 '05 #5
>> "Edwin" == Edwin Young <ed***@localhos t.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.b sd.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.Pixb uf(gtk.gdk.COLO RSPACE_RGB,
has_alpha=1, bits_per_sample =8,
width=rows, height=cols)
try: pa = pb.get_pixels_a rray()
except AttributeError: pa = pb.pixel_array

pa[:,:,:] = X

gc = self.new_gc()
pb.render_to_dr awable(self.gdk Drawable, 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
Jul 18 '05 #6
>>>>> "Edwin" == Edwin Young <ed***@localhos t.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

Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3355
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 progressbar and ListStore: basically i need to make pulse the progressbar while i'm copying files and write filenames copied on the ListStore on to the window.
0
1359
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 now) The PyGTK FAQ (pygtk.org) has some suggestions, but they have failed to clear up the issue. Using their suggestions as a starting point, I've arrived at the below. However, it fails in that I can't seem to
0
2029
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, but everything seems to assume that I'm either creating a window to put the menu in (I just want it to be the menu floating by itself) or that I want a button press to trigger the menu appearing (it should just pop into existence when I run it). ...
3
2594
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 apparently can't write XPMs. GTK will only take XPMs, that I can see. Therein lies the rub. I can ship over monochrome bitmaps via XBM, but I'd rather be able to ship over full color. (Use case, in case it matters: I am trying to embed a graphic into a...
0
1290
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 : http://www.pygtk.org/pygtk2tutorial/examples/filelisting.py) I'd like to be able to use a virtual list (like wx.ListCtrl in mode LC_VIRTUAL) ...
25
4277
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 on Windows "(...) > can i use pyGTK under > Windows???
5
8965
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 pygtk.require('2.0') import gtk
1
1718
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 but cant seem to do that and I have tried to pack widgets into an image but gtk.Image does not have the add attribute. What approach should I be taking?
2
2778
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 any way to display the image without saving it in a temporary file? I've tried to inherit from StringIO to make a file-like object, but gtk.gdk.pixbuf_new_from_file only takes the file name, as do similar GTK functions, and new_from_data wants RGB...
0
8712
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9177
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9077
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9021
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7954
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6637
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4465
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3159
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2524
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.