title: Python Image Ideal High/Low pass filter in frequency domain
body: <p>I need to implement a Image Low/High pass filer in frequency domain for educational purposes in college. 
  The algorithm I'm applying is:</p>
  
  <p>a) Perform the image centering transform on the original image<br> 
  b) Perform the DFT transform<br> 
  c) Alter the Fourier coefficients according to the required filtering<br> 
  d) Perform the IDFT transform<br> 
  e) Perform the image centering transform again (this undoes the first centering transform).<br></p>
  
  <p>Here is my code so far<br></p>
  
  <pre><code>import struct
  import numpy as np
  from math import pow
  from cmath import exp, pi
  
  imIn = open("before.bmp", "rb")
  
  #Get informations from .bmp file section#
  print 'Identifier: ', imIn.read(2)  
  print 'File size: ', struct.unpack('i',imIn.read(4))[0]
  print 'Reserved: ', struct.unpack('i', imIn.read(4))[0]
  offset = struct.unpack('i', imIn.read(4))[0]
  print 'Data offset: ',offset
  print 'Header size: ',struct.unpack('i', imIn.read(4))[0]
  width = struct.unpack('i', imIn.read(4))[0]
  print 'Width: ',width
  height = struct.unpack('i', imIn.read(4))[0]
  print 'Height: ',height
  #-------------------------------------------#
  
  
  #back to zero and write header section which is copied from the original file
  imIn.seek(0)
  header = imIn.read(offset)
  #----------------------------------------------------------------------------#
  
  
  # build data array
  imArr = []
  count = 0
  for i in range(0,width):
      row = []
      for j in range(0,height):
          tmp = imIn.read(1)
          val = struct.unpack('c', tmp)[0]
          row.append(val)
      imArr.append(row)
  #------------------#
  
  
  # write header information
  imOut = open("after.bmp","wb")
  imOut.write(header)
  #-------------------------#
  ArrInt =[]
  
  for i in range(0,width):
      row = []
      for j in range(0,height):
          row.append(ord(imArr[i][j]))
      ArrInt.append(row)
  del(imArr)
  
  #center image
  for i in range(0,width):
      row = []
      for j in range(0,height):
          ArrInt[i][j] = pow((-1),i*j)*ArrInt[i][j]
   #--------------------------------
  
  #fourier transformation
  iArr =  np.fft.fft2(ArrInt)
  #---------------------------------------
  
  #filer aplication(low pass filter)
  R = 1000 #distance
  for i in range(0,width):
      for j in range(0,height):
          temp = pow(float(height)/float(2)-i,2) + pow(float(width)/float(2)-j,2)
          if temp &gt; pow(R,2):
              iArr[i][j] = 0
          elif temp &lt;= pow(R,2):
              iArr[i][j] = iArr[i][j].real
  #-----------------------------------
  
  #fourier inverse transformation
  iArr =  np.fft.ifft2(iArr)
  #------------------------------
  
  #center image
  for i in range(0,width):
      row = []
      for j in range(0,height):
          iArr[i][j] = pow((-1),i*j)*iArr[i][j]
  #---------------------------------------
  
  for i in range(0,width):
      for j in range(0,height):
          if int(iArr[i][j].real) &gt; 255:
              imOut.write(struct.pack('c',chr(int(255))))
          elif int(iArr[i][j].real) &lt; 0:
              imOut.write(struct.pack('c',chr(int(0))))
          else: imOut.write(struct.pack('c',chr(int(iArr[i][j].real))))
  
  #Close files section#
  imIn.close()
  imOut.close()
  #-------------------#
  </code></pre>
  
  <p><br>
  My problem is: I don't get the expected result and I can't figure out what I did wrong.
  These are my input and output images:<br>
  <a href="http://i.stack.imgur.com/C3v4z.png" rel="nofollow">Input image</a><br>
  <a href="http://i.stack.imgur.com/7ZYvA.png" rel="nofollow">Output image</a></p>