[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] using pil to covert rgb into grayscale



On Thu, Apr 29, 2010 at 5:36 PM, Casey Duncan <casey@xxxxxxxxxxx> wrote:
> If you are going to roll your own, at least use the standard luminance calculation:
>
> L = 0.3 * red + 0.59 * green + 0.11 * blue
>
> hth,
Thanks for the quick reply guys.
I combined your answers and it works perfectly :-)

Here's the code I'm using:

def grayscale_image(surf):
    width, height = surf.get_size()
    for x in range(width):
        for y in range(height):
            red, green, blue, alpha = surf.get_at((x, y))
            L = 0.3 * red + 0.59 * green + 0.11 * blue
            gs_color = (L, L, L, alpha)
            surf.set_at((x, y), gs_color)
    return surf

Stas

-- 
Free-source educational programs for schools
http://www.schoolsplay.org  and http://wiki.laptop.org/go/Schoolsplay
http://gvr.sf.net and http://wiki.laptop.org/go/Guido_van_Robot