'''
Created on Oct 19, 2015

@author: ola 
'''
import Image

def makeGray(pixel):
    (r,g,b) = pixel
    gray = (r+g+b)/3
    return (gray,gray,gray)

def grayit2(picname):
    im = Image.open(picname)
    im.show()
    pixels = [makeGray(pix) for pix 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)
    (width,height) = im.size
    print "dimensions", width,height
    nim = im.copy()
    pix = nim.load()
    im.show()
    
    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")