title: How to convert a grayscale image into a list of pixel values?
body: <p>I am trying to create a python program which takes a grayscale, 24*24 pixel image file (I haven't decided on the type, so suggestions are welcome) and converts it to a list of pixel values from 0 (white) to 255 (black).</p>
  
  <p>I plan on using this array for creating a <a href="http://yann.lecun.com/exdb/mnist/" rel="nofollow noreferrer">MNIST</a>-like bytefile of the picture, that can be recognized by Tensor-Flow handwriting recognition algorithms.</p>
  
  <p>I have found the <a href="http://pillow.readthedocs.io/" rel="nofollow noreferrer">Pillow library</a> to be the most useful in this task, by iterating over each pixel and appending its value to an array</p>
  
  <pre><code>from PIL import Image
  
  img = Image.open('eggs.png').convert('1')
  rawData = img.load()
  data = []
  for y in range(24):
      for x in range(24):
          data.append(rawData[x,y])
  </code></pre>
  
  <p>Yet this solution has two problems:</p>
  
  <ol>
  <li>The pixel values are not stored as integers, but pixel objects which cannot be further mathematically manipulated and are therefore useless. </li>
  <li>Even the <a href="https://pillow.readthedocs.io/en/latest/reference/PixelAccess.html#pixelaccess-class" rel="nofollow noreferrer">Pillow docs</a> state that:
  
  <blockquote>
    <p>Accessing individual pixels is fairly slow. If you are looping over all of the pixels in an image, there is likely a faster way using other parts of the Pillow API.</p>
  </blockquote></li>
  </ol>