View Single Post
Old Dec 12th, 2005, 9:39 AM   #1
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,024
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Grayscale Converter with Pygame

Yeah, I was bored, so I made a pic --> grayscale program in Pygame.

fileName  = "python.gif"
red       = False
green     = True
blue      = False
########################

from pygame.image import load, save

pic = load(fileName)

for x in range(pic.get_width()):
    for y in range(pic.get_height()):

        c = list(pic.get_at((x,y))[:3])
        deg = (c[0]*red + c[1]*green + c[2]*blue) / (red + green + blue)
        
        pic.fill( [deg]*3, ((x,y),(1,1)) )

save(pic, "%s_%s%s%s.bmp"%(fileName.split('.')[0],
                           'R'*red,
                           'G'*green,
                           'B'*blue))

The user can select if it measures the degree based on R, G or B. Or any combination of the latter. A good example of manipulating boolean values I guess.

Here's an example: http://netpub.cstudies.ubc.ca/oleary/images/python.gif

Outputs: http://img204.imageshack.us/img204/8374/pythong4qm.png

I think it actually works quite beautifully.

Bleeeeh. ^_^
Sane is offline   Reply With Quote