# A square blur

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

  # loop for all pixels
  for x in range(2,getWidth(source)):
    for y in range(2,getHeight(source)):
      # get required pixels
      topleft =      getPixel(source, x-1, y-1)
      topright =     getPixel(source, x+1, y-1)
      topmiddle =    getPixel(source, x, y-1)
      bottomleft =   getPixel(source, x-1, y+1)
      bottomright =  getPixel(source, x+1, y+1)
      bottommiddle = getPixel(source, x, y+1)
      middleleft =   getPixel(source, x-1, y)
      middleright =  getPixel(source, x+1, y)
      center =       getPixel(target, x, y)

      # Use the '\' character to tell Python that
      # a commend continues on the next line
      # Set the new red color value
      newRed = (getRed(topleft) + getRed(topright) + \
                getRed(topmiddle) + getRed(bottomleft) + \
                getRed(bottomright) + getRed(bottommiddle) + \
                getRed(middleleft) + getRed(middleright) + \
                getRed(center)) / 9
      # Set the new green color value
      newGreen = (getGreen(topleft) + getGreen(topright) + \
                  getGreen(topmiddle) + getGreen(bottomleft) + \
                  getGreen(bottomright) + getGreen(bottommiddle) + \
                  getGreen(middleleft) + getGreen(middleright) + \
                  getGreen(center)) / 9
      # Set the new blue color value
      newBlue = (getBlue(topleft) + getBlue(topright) + \
                 getBlue(topmiddle) + getBlue(bottomleft) + \
                 getBlue(bottomright) + getBlue(bottommiddle) + \
                 getBlue(middleleft) + getBlue(middleright) + \
                 getBlue(center)) / 9
      # Set the new color of the center pixel
      setColor(center, makeColor(newRed, newGreen, newBlue))

  return target
