[sugar] how to make a grayscale image?

Erik Blankinship erikb
Thu Nov 1 21:29:53 EDT 2007


thanks everyone!

import gtk

def grayScalePixBuf( pb, copy ):
    arr = pb.get_pixels_array()
    if (copy):
        arr = arr.copy()
    for row in arr:
        for pxl in row:
            y = 0.3*pxl[0][0]+0.59*pxl[1][0]+0.11*pxl[2][0]
            pxl[0][0] = y
            pxl[1][0] = y
            pxl[2][0] = y
    return gtk.gdk.pixbuf_new_from_array(arr, pb.get_colorspace(),
pb.get_bits_per_sample())

pb = gtk.gdk.pixbuf_new_from_file("cat.jpg")
pb = grayScalePixBuf(pb, True)
pb.save( "grayCat.png", "png", {} )


On 11/1/07, Benjamin M. Schwartz <bmschwar at fas.harvard.edu> wrote:
>
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Erik Blankinship wrote:
> > Anyone know of an easy technique for turning a pixbuf or cairo context
> > into grayscale?
>
> I can't tell what you mean.  Do you mean to take a snapshot of an existing
> pixbuf, convert that snapshot to grayscale, and display the snapshot over
> the
> existing pixbuf?  If so, the easiest way I know of is
> gtk.gdk.Pixbuf.get_pixels_array and gtk.gdk.pixbuf_new_from_array[1].  These
> methods allow you to do math on the pixels as a Numeric Python array, and
> then
> make a new Pixbuf from the resulting array.
>
> 1.
>
> http://www.pygtk.org/docs/pygtk/class-gdkpixbuf.html#method-gdkpixbuf--get-pixels-array
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.7 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>
> iD8DBQFHKmkCUJT6e6HFtqQRAgrjAJ47VHqmT9j7xVAyNzgKXdgDTxQWMQCffJ6k
> D26RDImuualfzAdlJNaP4k0=
> =Oc7S
> -----END PGP SIGNATURE-----
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.laptop.org/pipermail/sugar/attachments/20071101/d795bd65/attachment.htm 



More information about the Sugar-devel mailing list