Images can be read into numpy, and are stored as multi-dimensional arrays.
#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:
im = imread('bieber.jpg')
imshow(im)
im
is a 3-D array:
im.shape
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:
im[4, 5, :]
The pixel intensity values are of type uint8
, meaning they range from 0
to 255
.
We can get the red channel like this:
r = im[:, :, 0]
imshow(r, cmap=cm.gray)
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?)
imshow(im[:,:,1], cmap=cm.gray)