title: Denoise image algorithm using Python
body: <p>I'm trying to write a program that "denoises" a picture. I'm trying to grab only the non-border pixels and use the bordering pixels to set it's value, but it seems like my algorithm doesn't grab the intended pixels (I get a screwed up image as a result). If I understood correctly n=length, m=height (e.g. rows). I'm aware that I can use numpy to get a 3d-representation, but that isn't what I'm trying to do. Can anyone see what I'm doing wrong? Here's the code:</p>
  
  <pre><code>from PIL import Image
  def DenoiseImage(inputIm, factor, iters):
  
  data = list(inputIm.getdata())
  n, m = inputIm.size
  data2 = data
  
  for y in range (0, iters):
  
      #operate on pixels that aren't part of the top or bottom row
      for x in range (n, ((m-1)*n)-1):
  
          #makes sure x operated on isn't part of the left or right border
          if (x-1)%n != 0 and x%n != 0:
              #denoise pixel
              data[x] = data2[x] +factor*(data2[x-1]+data2[x+1]-4*data[x]+data2[x-n]+data2[x+n])
  
      temp = data2
      data2 = data
      data = temp
  
  if iters%2==0:
      return data2
  else:
      return data
  </code></pre>
  
  <p>This is the algorithm I'm trying to use (but it's written for a 2d-representation (and in C), obviously): <a href="http://pastebin.com/teEeUgFj" rel="nofollow noreferrer">http://pastebin.com/teEeUgFj</a></p>
  
  <p><strong>EDIT:</strong> Kinda a graphical representation of the thoughts behind the algorithm, bringing all my artistic talent to bear: </p>
  
  <p><img src="https://snag.gy/oeQ45.jpg" alt="derp"></p>
  
  <p>Keep in mind m is the length downwards, so in the picture m would be = 4, not 301.</p>