## Reading and Displaying Images using NumPy¶

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

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

from numpy import *
from matplotlib.pyplot import *


Reading an image in NumPy works like this:

In [2]:
im = imread('bieber.jpg')

In [3]:
imshow(im)

Out[3]:
<matplotlib.image.AxesImage at 0x7ff1efa65e50>

im is a 3-D array:

In [4]:
im.shape

Out[4]:
(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 [5]:
im[4, 5, :]

Out[5]:
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 [6]:
r = im[:, :, 0]

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

Out[7]:
<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 [8]:
imshow(im[:,:,1], cmap=cm.gray)

Out[8]:
<matplotlib.image.AxesImage at 0x7ff1ef87b150>