## Reading and Displaying Images using NumPy¶

Images can be read into numpy, and are stored as multi-dimensional arrays.

In :
#Needed to display images inline in Jupyter
%matplotlib inline
####################################

from numpy import *
from matplotlib.pyplot import *
from scipy.misc import imread


Reading an image in NumPy works like this:

In :
im = imread('bieber.jpg')

In :
imshow(im)

Out:
<matplotlib.image.AxesImage at 0x7ff1efa65e50> im is a 3-D array:

In :
im.shape

Out:
(705, 940, 3)

For example,

im[4, 5, 0] is the red component of the pixel in the upper-left corner at coordinates (4, 5)

im[4, 5, 1] is the green component of the pixel in the upper-left corner at coordinates (4, 5)

im[4, 5, 2] is the blue component of the pixel in the upper-left corner at coordinates (4, 5)



We can get the whoe pixel like this:

In :
im[4, 5, :]

Out:
array([ 3,  3, 11], dtype=uint8)

The pixel intensity values are of type uint8, meaning they range from 0 to 255.

We can get the red channel like this:

In :
r = im[:, :, 0]

In :
imshow(r, cmap=cm.gray)

Out:
<matplotlib.image.AxesImage at 0x7ff1ef988590> As you can see, in the red channel, the maple leaf is almost invisible (why?)

On the other hand, in the green channel, the maple leaf is black. (Why?)

In :
imshow(im[:,:,1], cmap=cm.gray)

Out:
<matplotlib.image.AxesImage at 0x7ff1ef87b150> 