title: Convert an image to 2D array in python
body: <p>I want to convert an image to 2D array with 5 columns where each row is of the form <code>[r, g, b, x, y]</code>. x, y is the position of the pixel and r,g,b are the pixel values. (I will be using this array as input to a machine learning model). Is there a more efficient implementation than this in python?</p>
  
  <pre><code>import Image
  import numpy as np
  
  im = Image.open("farm.jpg")
  col,row =  im.size
  data = np.zeros((row*col, 5))
  pixels = im.load()
  for i in range(row):
      for j in range(col):
          r,g,b =  pixels[i,j]
          data[i*col + j,:] = r,g,b,i,j
  </code></pre>