title: Smoothing the pixels of an enlarged image
body: <p>I'm very new to programming, trying to learn Python. I am trying to smooth the pixels of an image by taking an average of each pixel and its neighbors. I was able to run the following successfully.</p>
  
  <pre><code>from cImage import *
  import statistics as s
  
  
  def smoothImg(oldimage):
      theWindow = ImageWin("Smooth Image",1200,1200)
      theImage = FileImage(oldimage)
      theImage.draw(theWindow)
  
      height = theImage.getHeight()
      width = theImage.getWidth()
  
      newImage = EmptyImage(width,height)
  
      for row in range(height):
          for col in range(width):
              try:
                  pixelVal = theImage.getPixel(col-1,row-1)
              except:
                  pixelVal = theImage.getPixel(col,row)  #maybe change to "pass"
              try:
                  pixelVal2 = theImage.getPixel(col-1,row)
              except:
                  pixelVal2 = theImage.getPixel(col,row)
              try:
                  pixelVal3 = theImage.getPixel(col-1,row+1)
              except:
                  pixelVal3 = theImage.getPixel(col,row)
              try:
                  pixelVal4 = theImage.getPixel(col,row-1)
              except:
                  pixelVal4 = theImage.getPixel(col,row)
              pixelVal5 = theImage.getPixel(col,row)
              try:
                  pixelVal6 = theImage.getPixel(col,row+1)
              except:
                  pixelVal6 = theImage.getPixel(col,row)
              try:
                  pixelVal7 = theImage.getPixel(col+1,row-1)
              except:
                  pixelVal7 = theImage.getPixel(col,row)
              try:
                  pixelVal8 = theImage.getPixel(col+1,row+1)
              except:
                  pixelVal8 = theImage.getPixel(col,row)
              try:
                  pixelVal9 = theImage.getPixel(col+1,row)
              except:
                  pixelVal9 = theImage.getPixel(col,row)
  
              redMean = s.mean([pixelVal[0],pixelVal2[0],pixelVal3[0],pixelVal4[0],pixelVal5[0],pixelVal6[0],pixelVal7[0],pixelVal8[0],pixelVal9[0]])
              greenMean = s.mean([pixelVal[1],pixelVal2[1],pixelVal3[1],pixelVal4[1],pixelVal5[1],pixelVal6[1],pixelVal7[1],pixelVal8[1],pixelVal9[1]])
              blueMean = s.mean([pixelVal[2],pixelVal2[2],pixelVal3[2],pixelVal4[2],pixelVal5[2],pixelVal6[2],pixelVal7[2],pixelVal8[2],pixelVal9[2]])
  
              meanPixel = Pixel(redMean,greenMean,blueMean)
              newImage.setPixel(col,row,meanPixel)
  
  
      newImage.setPosition(width+1,0)
      newImage.draw(theWindow)
  
      theWindow.exitOnClick()
      return
  </code></pre>
  
  <p>The program seems to work, but I can't imagine that this is the best way to do it. Also I'd like to be able to take a wider average of pixels instead of just an immediate neighbor. I tried running this function within my for loops but it just gave me a completely gray image.</p>
  
  <pre><code>def getMean(numPixels,image):
          pixelVals = []
          for row in range(numPixels):
              for col in range(numPixels):
                  pixelVal = image.getPixel(col,row)
                  pixelVals.append(pixelVal)
          redMean = int(s.mean([p[0] for p in pixelVals]))
          greenMean = int(s.mean([p[1] for p in pixelVals]))
          blueMean = int(s.mean([p[2] for p in pixelVals]))
          meanPixel = Pixel(redMean,greenMean,blueMean)
          return meanPixel
  </code></pre>
  
  <p>This problem is taken from "Python Programming in Context". Exercise 6.25 on page 222.</p>