
from pylab import *
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
import random
import time
from scipy.misc import imread
from scipy.misc import imresize
import matplotlib.image as mpimg
import os

#os.chdir('c:/Users/Guerzhoy/Desktop/csc320/pca_example/')

###################
#Run the following for grayscale images to show up correctly and for images
#to display immediately
#%matplotlib
#gray()
########################



a = array([5, 4, 10])  #1-d array, like a list

a = array([[4,5,6], [1,2,3]]) #2-d array

####################
#Matrix slicing, accessing elements, etc.:
''''''''''''''''

In [5]: a[0]
Out[5]: 5

In [6]: a.shape
Out[6]: (3L,)

In [7]: a = array([[4,5,6], [1,2,3]])

In [8]: a
Out[8]: 
array([[4, 5, 6],
       [1, 2, 3]])

In [9]: a.shape
Out[9]: (2L, 3L)

In [10]: a[1,2]
Out[10]: 3

In [11]: a[:, 2]
Out[11]: array([6, 3])

In [12]: a[1, :]
Out[12]: array([1, 2, 3])
''''''''''''''''

#Read in an image
i = imread('bieber.jpg')

'''
In [15]: i
Out[15]: 
array([[[  1,   1,   9],
        [  2,   2,  10],
        [  2,   2,  10],
        ..., 
        [160,   4,  18],
        [154,   6,  18],
        [163,   8,  22]],

       [[  2,   2,  10],
        [  2,   2,  10],
        [  2,   2,  10],
        ..., 
        [160,   4,  18],
        [154,   6,  18],
        [162,   7,  21]],

       [[  2,   2,  10],
        [  2,   2,  10],
        [  2,   2,  10],
        ..., 
        [160,   4,  18],
        [155,   5,  17],
        [162,   6,  20]],

       ..., 
       [[ 49,  31,  11],
        [ 52,  31,  10],
        [ 58,  33,  11],
        ..., 
        [126,  15,  21],
        [126,  15,  21],
        [126,  15,  21]],

       [[ 50,  32,   8],
        [ 54,  34,  10],
        [ 61,  37,  11],
        ..., 
        [129,  16,  22],
        [128,  15,  21],
        [128,  15,  21]],

       [[ 57,  34,  16],
        [ 60,  35,  13],
        [ 69,  39,  13],
        ..., 
        [130,  20,  23],
        [131,  21,  24],
        [130,  20,  23]]], dtype=uint8)
'''


#Display the image, access individual pixels, get the red/green/blue channel
'''
In [14]: imshow(i)
Out[14]: <matplotlib.image.AxesImage at 0xa23aa90>


In [16]: i.shape
Out[16]: (705L, 940L, 3L)

In [17]: i[200, 200, :]
Out[17]: array([122, 104,  94], dtype=uint8)

In [18]: i[300, 200, 2]
Out[18]: 214

In [19]: r = i[:,:, 0]

In [20]: g = i[:,:,1]

In [21]: b = i[:,:,2]
'''

#Display the different colour channels
'''
In [23]: figure(2); imshow(r)
Out[23]: <matplotlib.image.AxesImage at 0xa63d7f0>

In [24]: figure(3); imshow(g)
Out[24]: <matplotlib.image.AxesImage at 0x10d02278>

In [25]: figure(4); imshow(b)
Out[25]: <matplotlib.image.AxesImage at 0x10fe0a20>
'''

#Create a new array with the same size/dimensions as i
z = zeros(i.shape).astype(uint8)


#Display the image with the blue channel zeroed-out
z[:,:, 0] = r
z[:,:, 1] = g
imshow(z)

#A darker image
.7*z #note: this won't display as a darker image by default since imshow() normalizes grayscale images. See contrast.py for hwo
     #to display darker images
     

u = array([1,2,3])
v = array([5,4,6])

#Aside: 
'''
In [39]: zip(u, v)
Out[39]: [(1, 5), (2, 4), (3, 6)]
'''

#Slow way to compute the dot product
s = 0
for c_u, c_v in zip(u, v):
    s += c_u*c_v
    
#The same thing, but much faster
s = np.dot(u, v) 


#much better and faster to go 
u+v

#than
s = zeros(u.shape)
for i in range(u.shape[0]):
    s[i] = u[i]+v[i]
    
