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.
            Asked
            
        
        
            Active
            
        
            Viewed 1,527 times
        
    3
            
            
        
        freundTech
        
- 111
 - 9
 
2 Answers
2
            
            
        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
        cox
        
- 731
 - 5
 - 12
 
- 
                    Ok... Thats weird. I can access individual pixels like that, but it looks like there are 2 extra byte per row of pixels. This happens only when the pixbuf is a screenshot from my first monitors (768x1366) but not on screenshots from my 1050x1680 monitor. Even if I only have one monitor connected. – freundTech Apr 18 '14 at 10:12
 - 
                    I found out a bit more: I think it only works, if the width of the image is dividable by 4. Not sure why. I think this is a bug... – freundTech Apr 18 '14 at 14:22
 - 
                    No. it is not a bug. It may be the „stride”, but can't be sure without see every thing. Can you put the data in a PIL image and modify that extra, maybe is visible where come from? – cox Apr 21 '14 at 08:24
 
1
            
            
        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
        am70
        
- 591
 - 6
 - 8