title: Saving a Numpy array as an image (instructions)
body: <p>I found my answer in a previous post: <a href="https://stackoverflow.com/questions/902761/saving-a-numpy-array-as-an-image">Saving a Numpy array as an image</a>.  The only problem being, there isn't much instruction on using the PyPNG module.</p>
  
  <p>There are only a few examples online--
  <a href="http://packages.python.org/pypng/ex.html#numpy" rel="noreferrer">http://packages.python.org/pypng/ex.html#numpy</a>
  <a href="http://nullege.com/codes/search/png.Writer.write" rel="noreferrer">http://nullege.com/codes/search/png.Writer.write</a></p>
  
  <p>But what do I do in light of .write errors like this:</p>
  
  <pre><code>Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/png.py", line 638, in write
      nrows = self.write_passes(outfile, rows)
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/png.py", line 783, in write_passes
      extend(row)
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/png.py", line 780, in &lt;lambda&gt;
      return lambda sl: f(map(int, sl))
  TypeError: argument 2 to map() must support iteration
  </code></pre>
  
  <p>Here's where the error happens in my code, PCA_tool.py (The error comes after "folder.write(outfilename, PrincipalComponent"):</p>
  
  <pre><code>#PrincipalComponent.save(path+'transform_'+str(each)+'.png', format='PNG')
  outfilename = open(str(path)+'transformed/transform_'+str(each)+'.png', 'wb')
  folder = png.Writer(m,n,greyscale=True)
  folder.write(outfilename, PrincipalComponent)
  outfilename.close()
  
  sys.exit(0)
  </code></pre>
  
  <p>I'm trying to save a 8400 element numpy.ndarray as a n=80 column, m=105 row greyscale png image.</p>
  
  <p>Thanks,</p>