'''
Created on Oct 30, 2017

@author: rodger 
'''
from PIL import Image

def makeGreen(pixel):
    (r,g,b) = pixel
    if r==255 and g==255 and b==255: # must be white pixel
        pass
    else:  # must be a blue pixel
        r = 0
        g = 230
        b = 0 
    return (r,g,b)

def greenit2(picname):
    im = Image.open(picname)
    print "im is of type", type(im)
    im.show()
    #pixels = [makeGray(pix) for pix in im.getdata()]
    pixels = [makeGreen((x,y,z)) for (x,y,z) in im.getdata()]
    nim = Image.new("RGB",im.size)
    nim.putdata(pixels)
    nim.show()
    nim.save("green"+picname)

def makeGray(pixel):
    (r,g,b) = pixel
    gray = (r+g+b)/3
    return (gray,gray,gray)

def grayit2(picname):
    im = Image.open(picname)
    print "im is of type", type(im)
    im.show()
    #pixels = [makeGray(pix) for pix in im.getdata()]
    pixels = [makeGray((x,y,z)) for (x,y,z) in im.getdata()]
    nim = Image.new("RGB",im.size)
    nim.putdata(pixels)
    nim.show()
    nim.save("gray"+picname)

def grayit(picname):
    im = Image.open(picname)
    im.show()
    (width,height) = im.size
    print "dimensions", width,height
    nim = im.copy()
    pix = nim.load()
    
    for x in range(width):
        for y in range(height):
            (r,g,b) = pix[x,y]
            gray = (r+g+b)/3
            pix[x,y] = (gray,gray,gray)
    nim.show()
    nim.save("gray"+picname)


if __name__ == '__main__':
    #grayit("eastereggs.jpg")
    #grayit2("bluedevil.png")
    greenit2("bluedevil.png")
