Cuylar Conly
Cuylar Conly

Reputation: 685

Image to Array: Plain English

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:

ab = numpy.asarray(img.convert('L'))

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

Thanks

Upvotes: 0

Views: 174

Answers (1)

rjonnal
rjonnal

Reputation: 1207

What is a digital image?

A digital image is a set of pixel values. Consider this image: small smiley face. 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.

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

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

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

Why would you want to convert a digital image to a numpy array?

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 (ab_mean = numpy.mean(ab) or ab_var = numpy.var(ab)), FFT it (ab_fft = numpy.fft.fft2(ab)), or transpose it (ab_transposed = ab.T):small transposed smiley face

Upvotes: 2

Related Questions