title: Implement dilation filter in PGM format files
body: <p>I'm currently doing a student project which does some image processings (dilation and erosion) on greyscale pgm file. I tried to implement dilation on the image object but got weird result. I expected the image object will be larger than the original image. But I got multiple copies of the object depending the kernel size I use.</p>
  
  <p>Original Picture: </p>
  
  <p><a href="https://i.stack.imgur.com/RyHPe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RyHPe.png" alt="Original Picture"></a></p>
  
  <p>Dilation result:</p>
  
  <p><a href="https://i.stack.imgur.com/cCpUk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cCpUk.png" alt="Dilation Result"></a></p>
  
  <p>This is my dilation source code</p>
  
  <pre><code>import numpy as np
  
  
  def pgm_read(filename):
      """Read PGM file to a array"""
      # Performance Bottleneck: I/O system calls, parallel write/read
      try:
          with open(filename, 'r') as fp:
              lines = fp.readlines()
              header_info = lines[2].split()
              return np.array([line.strip('\n') for line in lines[4:]], dtype=np.int32).reshape(int(header_info[0]),
                                                                                                int(header_info[1]))
      except OSError:
          print("An exception occurred")
  
  
  def pgm_write(img, dest, header):
      """Write numpy array to PGM file"""
      try:
          header = "P2\n# test\n" + header + "\n80\n"
  
          f = open(dest, "w")
          f.write(header)
          f.close()
          with open(dest, "a") as f:
              for x in range(img.shape[0]):
                  for y in range(img.shape[1]):
                      f.write(str(img[x][y]) + "\n")
      except OSError:
          print("Writing exception occurred")
  
  
  def dilation(img):
  
      rows, cols = img.shape
      dilated_img = np.zeros((rows, cols), dtype=np.int32)
  
      kernel = np.ones((2, 2))
      rows2, cols2 = kernel.shape
  
      for x in range(rows):
          for y in range(cols):
              """Search the object within the image"""
              if img[x][y] == 1:
                  # Convolve with kernel
                  for i in range(rows2):
                      for j in range(cols2):
                          if kernel[i][j] == 1:
                              # Object Enlargement
                              c = x + i
                              d = y + j
                              if c &lt; rows and d &lt; cols:
                                  dilated_img[c][d] = 1
  
      for x in range(rows):
          for y in range(cols):
              """Give the object brightest colour for debugging purpose"""
              if dilated_img[x][y] == 1:
                  dilated_img[x][y] = 80
  
      return dilated_img
  
  
  if __name__ == '__main__':
      a = pgm_read("Axial_68.pgm")
      a = dilation(a)
      target = "Axial_68_1.pgm"
      header = "265 490"
      pgm_write(a, target, header)
  </code></pre>
  
  <p><a href="https://drive.google.com/file/d/1V8YdaoZXT5QRDMNZ9ExI7AzQ_g62Cbby/view?usp=sharing" rel="nofollow noreferrer">Axial_68.pgm</a></p>
  
  <p>I am very certain my file reading and writing functions work properly as I can use it to read and write other source pgm file properly.</p>
  
  <p>And one thing I found very weird behaviour is I could print half pgm file like this.</p>
  
  <p><a href="https://i.stack.imgur.com/019N6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/019N6.png" alt="Half Horizontal"></a></p>
  
  <p>using code</p>
  
  <pre><code>    for x in range(rows // 2):
          for y in range(columns):
              arr[x][y] = 80
  </code></pre>
  
  <p>But when I use this code and expect half vertical using code:</p>
  
  <pre><code>    for x in range(rows):
          for y in range(columns // 2):
              arr[x][y] = 80
  </code></pre>
  
  <p>I got this:</p>
  
  <p><a href="https://i.stack.imgur.com/A0HAX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/A0HAX.png" alt="Half vertical"></a></p>
  
  <p>I have tried this with a few other generated pgm files and all are same result.
  I wonder if my dilation code has something to do with this weird behaviour.</p>