title: Properly convert png to npy numpy array (Image to Array)
body: <p>When I generate an image and then generate a numpy array from it, the original <code>.npy</code> file differs from the new one. I thought <code>new-array.npy</code> would be exactly the same as <code>original-array.npy</code> since they are coming from the same image.</p>
  <p>For an example, I used this little image with 4*4 pixels:<br />
  <a href="https://i.stack.imgur.com/gpSxj.png" rel="noreferrer">original-image.png</a></p>
  <p>Here is a larger version (not the one I'm working with):
  <a href="https://i.stack.imgur.com/QhEbW.png" rel="noreferrer"><img src="https://i.stack.imgur.com/QhEbW.png" alt="enter image description here" /></a></p>
  <p>The last part of the code is the one that converts the <code>.png</code> to <code>.npy</code>. I think the problem is in here somewhere.</p>
  <pre><code>import numpy as np
  from PIL import Image
  from matplotlib import pyplot as plt
  
  filename = 'image-test'
  
  img = Image.open( filename + '.png' )
  data = np.array( img, dtype='uint8' )
  
  np.save( filename + '.npy', data)
  
  # visually testing our output
  img_array = np.load(filename + '.npy')
  plt.imshow(img_array) 
  </code></pre>
  <p>My simple algorithm:</p>
  <ol>
  <li>Generate random rgb array and save it as <code>.npy</code></li>
  <li>Save a <code>.png</code> file from that numpy array.</li>
  <li>Load that <code>.png</code> file and save it back to <code>.npy</code></li>
  </ol>
  <pre><code>import numpy as np
  from matplotlib import pyplot as plt
  import matplotlib
  
  from PIL import Image                                                                                
  
  
  ####create a matrix of random colors
  filename = &quot;original-array&quot;
  
  matrix=np.random.random((4,4,3))
  nx,ny,nz=np.shape(matrix)
  CXY=np.zeros([ny, nx])
  for i in range(ny):
      for j in range(nx):
          CXY[i,j]=np.max(matrix[j,i,:])
  
  #Save binary data
  np.save(filename + '.npy', CXY)
  print(filename + &quot; was saved&quot;)
  
  #Load npy
  img_array = np.load(filename + '.npy')
  plt.imshow(img_array)
  
  
  ####Save npy as png
  filename = &quot;original-image&quot;
  
  img_name = filename +&quot;.png&quot;
  matplotlib.image.imsave(img_name, img_array)
  print(filename + &quot; was saved&quot;)
  
  
  #### Convert that png back to numpy array
  
  img = Image.open( filename + '.png' )
  data = np.array( img, dtype='uint8' )
  
  #Convert the new npy file to png
  filename = &quot;new-array&quot;
  
  np.save( filename + '.npy', data)
  print(filename + &quot; was saved&quot;)
  
  
  #Load npy
  img_array = np.load(filename + '.npy')
  
  filename = &quot;new-image&quot;
  #Save as png
  img_name = filename +&quot;.png&quot;
  matplotlib.image.imsave(img_name, img_array)
  print(filename + &quot; was saved&quot;)
  </code></pre>
  <p>Here is the result :
  <a href="https://i.stack.imgur.com/MFNXI.png" rel="noreferrer"><img src="https://i.stack.imgur.com/MFNXI.png" alt="enter image description here" /></a></p>
  <p>When I regenerate an image from <code>new-array.npy</code> I get exactly the same image as <code>original-image.png</code>:<br />
  <a href="https://i.stack.imgur.com/L7dus.png" rel="noreferrer"><img src="https://i.stack.imgur.com/L7dus.png" alt="enter image description here" /></a></p>