# Recipe 40: Create a simple line drawing using edge detection

def linedetect(filename):

  source = makePicture(filename)
  target = makePicture(filename)

  for x in range(1,getWidth(source)): #loop for all pixels
    for y in range(1,getHeight(source)):
      here = getPixel(target, x, y) #the location of the current pixel
      thereDown =  getPixel(source, x, y+1) #the pixel below the curent pixel
      thereRight = getPixel(source, x+1, y) #the pixel right of the current pixel

      # Use the '\' character to tell Python that
      # a commend continues on the next line
       #get the luminecence of the current pixel
      hereLum = (getRed(here) + getGreen(here) + \
                 getBlue(here)) / 3
      #get the luminecence of the pixel below the current pixel
      thereDownLum = (getRed(thereDown) + getGreen(thereDown) + \ 
                 getBlue(thereDown)) / 3
      #get the luminecence of the pixel to the right of the current pixel
      thereRightLum = (getRed(thereRight) + getGreen(thereRight) + \  
                 getBlue(thereRight)) / 3
      
      #if the difference between the luminecence of the pixels around the current pixel with the current pixel is greater than 2
      if (abs(hereLum - thereDownLum) > 4) and \ 
        (abs(hereLum - thereRightLum) > 4):
        setColor(here, black) #set the color of the current pixel to white
      else:
        setColor(here, white) #if the above condition is not met then set the color to black

  show(target)

  return target
