freundTech
freundTech

Reputation: 111

Python PyGObject Get pixel from GdkPixbuf.get_pixels()

I've written a python program using GTK+2 (pygtk). Now I want to update the program to GTK+3, so I have to use pyGObject instead of pygtk.
My problem is, that I have to get the color from individual pixels on a GdkPixbuf.Pixbuf object.
In pygtk i could just use Pixbuf.get_pixels_array() to get a n array containing all the pixels.
In pyGObject there is no Pixbuf.get_pixels_array(), so I have to use Pixbuf.get_pixels(), which returns me a string.
Does anyone know how to get individual pixels from this string?

(In C Pixbuf.get_pixels_array() returns a pointer, so you can do this: http://developer.gimp.org/api/2.0/gdk-pixbuf/gdk-pixbuf-gdk-pixbuf.html#image-data , but in python it returns a string)

Thanks for helping.

Upvotes: 3

Views: 1633

Answers (2)

am70
am70

Reputation: 729

In my answer to question How to turn Gdk pixbuf object into numpy array you may find two functions that convert from GdxPixbuf to numpy arrays, and back; once you have data into numpy arrays, it is much easier to manipulate them

Upvotes: 1

cox
cox

Reputation: 721

It is not a string, it is a byte array. You may get values (int) like from list: mybyte[2:4]; if you print this, yes, the chr() values are show. Be careful at image type and check the length of the „string”: for a png you have 4 values (RGBA) per pixel, for a 'RGB' jpeg - 3, for 'L' - 1. The values are in range 0-255, as usual. P.S: the values are not splited in bands, so pixel 1 from jpg has RGB at index 0,1,2 pixel 2 at 3,4,5 and so on

Upvotes: 2

Related Questions