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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With