# Recipe 39: A simple blur

def blur(filename):
  source = makePicture(filename)
  target = makePicture(filename)

  for x in range(2,getWidth(source)):
    for y in range(2,getHeight(source)):
      top =    getPixel(source, x, y-1)
      bottom = getPixel(source, x, y+1)
      left =   getPixel(source, x-1, y)
      right =  getPixel(source, x+1, y)
      centre = getPixel(target, x, y)

      # Use the '\' character to tell Python that
      # a commend continues on the next line
      newRed = (getRed(top) + getRed(bottom) + \
                getRed(left) + getRed(right) + \
                getRed(centre)) / 5
      newGreen = (getGreen(top) + getGreen(bottom) + \
                  getGreen(left) + getGreen(right) + \
                  getGreen(centre)) / 5
      newBlue = (getBlue(top) + getBlue(bottom) + \
                 getBlue(left) + getBlue(right) + \
                 getBlue(centre)) / 5
      setColor(centre, makeColor(newRed, newGreen, newBlue))

  return target
