Image to Array: Plain English
<p>I'm starting to use numpy and PILlow to work with image files.  And, generally loosing myself when it comes to converting images to arrays, and then working with arrays.
can someone explain what is happening when I convert an image to array. Such as this:</p>

<pre><code>ab = numpy.asarray(img.convert('L'))
</code></pre>

<p>And, also, why convert to an array? what functionality does this provide, what can I do with the array?</p>

<p>Thanks</p>

<p><strong>What is a digital image?</strong></p>

<p>A digital image is a set of pixel values. Consider this image:  <a href="http://i.imgur.com/wlDQhpL.png" rel="nofollow"><img src="http://i.imgur.com/wlDQhpL.png" alt="small smiley face"></a>. It consists of 16x16 pixels. Because most displays have 8 bits (2^8 (256) possible values) and 3 channels (for red, green, and blue), it consists of a 16x16x3 array of numbers between 0 and 255.</p>

<p>The digital image of the smile was <em>already</em> made of numbers. When you loaded it using <code>PIL.Image</code> and then converted to a numpy array, all you did was expose the numbers to numpy. The <code>convert('L')</code> call asks PIL to convert a color image (3 channels) into a <strong>L</strong>uminance or grayscale (1 channel) image. So instead of being exposed to numpy as a 16x16x3 matrix, it's exposed as a 16x16x1 array.</p>

<p>There's nothing special about the numbers in a digital image. In fact here are the numbers that comprise the digital image above:</p>

<pre><code>255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255   6   6   6   6   6   6   6 255 255 255 255
255 255 255   6   6   6   6   6   6   6   6   6   6 255 255 255
255 255   6   6   6 130 255 255 255 255 255   6   6   6 255 255
255 255   6   6 255 255 255 255 255 255 255 255   6   6   6 255
255   6   6  68 255 255 255 255 255 255 255 255 255   6   6 255
255   6   6 255 255   6 161 255 255 255   6   6 255  84   6   6
255   6   6 255  68   6   6 255 255 255   6   6 255 255   6   6
255   6   6 255 255   6 255 255 255 255   6 115 255 255   6   6
255   6   6 255 255 255 255 255 255 255 255 255 255 224   6   6
255   6   6 255 255   6   6   6   6   6   6 255 255   6   6 176
255 130   6   6 255 255   6   6   6   6  84 255 239   6   6 255
255 255   6   6   6 255 255 255 255 255 255 208   6   6 176 255
255 255 255   6   6   6   6 239 255 115   6   6   6   6 255 255
255 255 255 255   6   6   6   6   6   6   6   6 224 255 255 255
255 255 255 255 255 255 115   6   6   6 255 255 255 255 255 255
</code></pre>

<p><strong>Why would you want to convert a digital image to a numpy array?</strong></p>

<p>I don't know what Pillow's capabilities are, but I would guess that numpy's numerical/statistical/quantitative capabilities are much more powerful than Pillow's. Once you've exposed the numbers in the image to numpy, you can do all sorts of cool things, like find it's average luminance or variance (<code>ab_mean = numpy.mean(ab)</code> or <code>ab_var = numpy.var(ab)</code>), FFT it (<code>ab_fft = numpy.fft.fft2(ab)</code>), or transpose it (<code>ab_transposed = ab.T</code>):<a href="http://i.imgur.com/jV2Zy60.png" rel="nofollow"><img src="http://i.imgur.com/jV2Zy60.png" alt="small transposed smiley face"></a></p>

python arrays python-2.7 numpy pillow