[pygtk] image manipulation

Tim Evans t.evans at aranz.com
Mon Sep 11 07:30:30 WST 2006


Rodrigo Renie Braga wrote:
> ok, i'm doing some tests here with Numeric, and it seems that the 
> gtk.gdk.Pixbuf.get_pixels_array() returns an (Numeric) array with the 
> following shape: (w, h, 3), meaning a matrix with a dimension of WxHx3 ...
> 
> so, to access the rgb of a pixel on position (X, Y), i just use:
> 
> matrix[x][y][0] for red
> matrix[x][y][1] for green
> matrix[x][y][2] for blue

The correct indices are actually:

matrix[y][x][0] for red
matrix[y][x][1] for green
matrix[y][x][2] for blue

Think of the matrix as a sequence of rows, where each row is a sequence 
of pixels, and each pixel is a 3-element vector of RGB (or 4-element RGBA).

> is that right? And what is the best way to go through this matrix? maybe:
> 
> for i in matrix:
>     for j in i:
>         j[0] = color_red
>         j[1] = color_green
>         j[2] = color_blue
> 
> is this interaction changing the value of matrix? ( I don't lide the 
> looks of this loop.. :-) )

This will work; I'd just recommend using descriptive variable names to 
make it more obvious:

for row in matrix:
     for pixel in row:
         pixel[0] = color_red
         pixel[1] = color_green
         pixel[2] = color_blue

-- 
Tim Evans
Applied Research Associates NZ
http://www.aranz.co.nz/


More information about the pygtk mailing list