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

def linedetect(filename):

  source = makePicture(filename)
  target = makePicture(filename)
  
  # loop for all pixels in source picture
  for x in range(1,getWidth(source)):
    for y in range(1,getHeight(source)):
      # get required pixels
      here = getPixel(target, x, y)
      thereDown =  getPixel(source, x, y+1)
      thereRight = getPixel(source, x+1, y)

      # Use the '\' character to tell Python that
      # a commend continues on the next line
      # get the luminicity of the requierd pixels
      hereLum = (getRed(here) + getGreen(here) + \
                 getBlue(here)) / 3
      thereDownLum = (getRed(thereDown) + getGreen(thereDown) + \
                 getBlue(thereDown)) / 3
      thereRightLum = (getRed(thereRight) + getGreen(thereRight) + \
                 getBlue(thereRight)) / 3
      
      # set the new color of current pixel in the target picture
      if (abs(hereLum - thereDownLum) > 1) and \
         (abs(hereLum - thereRightLum) > 1):
        setColor(here, black)
      else:
        setColor(here, white)

  show(target)

  return target
