title: How to read PGM image in Python
body: <p>How to read this PGM image in python? please help </p>
  
  <p><a href="https://files.fm/u/aag76cpe" rel="nofollow noreferrer">PGM image link</a></p>
  
  <p>I tried this </p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  
  def readpgm(name):
      with open(name) as f:
           lines = f.readlines()
  
      # Ignores commented lines
      for l in list(lines):
              if l[0] == '#':
              lines.remove(l)
  
      # Makes sure it is ASCII format (P2)
      assert lines[0].strip() == 'P2' 
  
      # Converts data to a list of integers
      data = []
      for line in lines[1:]:
          data.extend([int(c) for c in line.split()])
  
      return (np.array(data[3:]),(data[1],data[0]),data[2])
  
  data = readpgm('514516.pgm')
  
  plt.imshow(np.reshape(data[0],data[1])) # Usage example
  </code></pre>
  
  <p>it gives me this error: </p>
  
  <pre><code>"return codecs.charmap_decode(input,self.errors,decoding_table)[0]
  
   UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 
   2075: character maps to &lt;undefined&gt;"
  </code></pre>
  
  <p>what should i do? please help</p>
  
  <p>Solution: </p>
  
  <p><a href="https://stackoverflow.com/questions/7368739/numpy-and-16-bit-pgm">Numpy and 16-bit PGM</a></p>