def chromakey(bg):
  bgh=getHeight(bg)
  bgw=getWidth(bg)
  greenPic=makePicture (pickAFile())

  xoff=requestInteger("x offset")
  yoff=requestInteger("y offset")
  for p in getPixels(greenPic):
    r =getRed( p )
    g =getGreen( p )
    b =getBlue( p )
    if (r + b > g): #if its NOT green
      x=getX(p)
      y=getY(p)
      if (y+yoff<bgh)and(x+xoff<bgw):  # Make sure the pixel exist in the bg pic
        nuColor=getColor(p)
        bgPixel=getPixel(bg, x+xoff,y+yoff)
        setColor(bgPixel,nuColor)





def invert(picture):
 collection=getPixels(picture)  
 for pixel in collection:
   r=getRed(pixel)
   g=getGreen(pixel)
   b=getBlue(pixel)
   r=255-r
   g=255-g
   b=255-b
   newColor=makeColor(r,g,b)
   setColor(pixel,newColor)


 


def grayscale(picture):
  collection=getPixels(picture)  
  for pixel in collection:
   r=getRed(pixel)
   g=getGreen(pixel)
   b=getBlue(pixel)
   grey=(r+g+b)/3
   newColor=makeColor(grey,grey,grey)
   setColor(pixel,newColor)






# Open a picture file.
nia=pickAFile()
asha=makePicture(nia)
show(asha)

# Use a FILTER here
choice=requestInteger("choose a Filter\n0=Greyscale 1=Interger 2=Chromakey")
if(choice==0):
  grayscale(asha)
if(choice==1):
  invert(asha)
if(choice==2):
   chromakey(asha)


# repaint the pic
repaint(asha)

# Now save the picture
path=requestString("Save picture as")
if(path!=''):
  folder=pickAFolder()
  pathway=folder + "\\" + fileName + ".jpg"
  printNow(pathway)

  # Now save it
  writePictureTo(asha,pathway)
