In [1]:
import matplotlib.pyplot as plt
import numpy as np
import time
In [2]:
## Task 0. Load and display my original colored face
my_face_colored = plt.imread('./my_face.jpeg')
#############################################################################
## NOTE: cmap='gray' is needed, because even when there is only one color, ##
##       imshow, by default, does not work with the 'gray' color map       ##
#############################################################################
plt.imshow(my_face_colored, cmap='gray')
plt.show()
In [3]:
## Task 1. Use GRAY = 0.3*RED + 0.6*GREEN + 0.1*BLUE to convert my colored face into my gray face
my_face_gray = 0.3 * my_face_colored[:,:,0] + 0.6 * my_face_colored[:,:,1] + 0.1 * my_face_colored[:,:,2]
plt.imshow(my_face_gray, cmap='gray')
plt.show()
In [4]:
## Task 2. Flip my original colored face horizontally -- i.e. with respect to the y-axis
my_face_colored = np.array(my_face_colored, copy=True)
my_face_flipped = np.array(my_face_colored, copy=True)

for i in range(my_face_colored.shape[1]):
    temp = my_face_flipped[:, i, :]
    my_face_flipped[:, i, :] = my_face_colored[:, (my_face_colored.shape[1]-1)-i, :]
    my_face_colored[:, (my_face_colored.shape[1]-1)-i, :] = temp

plt.imshow(my_face_flipped, cmap='gray')
plt.show()
In [5]:
## Task 3. Blur my_face_gray
#print(my_face_gray.shape)
my_face_gray_zeropadded = np.insert(my_face_gray, my_face_gray.shape[0], 0, axis=0)
my_face_gray_zeropadded = np.insert(my_face_gray_zeropadded, 0, 0, axis=0)
my_face_gray_zeropadded = np.insert(my_face_gray_zeropadded, my_face_gray.shape[1], 0, axis=1)
my_face_gray_zeropadded = np.insert(my_face_gray_zeropadded, 0, 0, axis=1)
#print(my_face_gray_zeropadded.shape)

num_iters = 100
start_time = time.time()
print('Begin blurring my gray face...')
print('0 iterations completed;')

for counter in range(num_iters):
    for i in range(1, my_face_gray.shape[0]):
        for j in range(1, my_face_gray.shape[1]):
            my_face_gray_zeropadded[i][j] = (1.0/8.0) * (my_face_gray_zeropadded[i-1][j-1]
                                                         + my_face_gray_zeropadded[i-1][j]
                                                         + my_face_gray_zeropadded[i-1][j+1]
                                                         + my_face_gray_zeropadded[i][j-1]
                                                         + my_face_gray_zeropadded[i][j+1]
                                                         + my_face_gray_zeropadded[i+1][j-1]
                                                         + my_face_gray_zeropadded[i+1][j]
                                                         + my_face_gray_zeropadded[i-1][j+1])
    if ((counter+1) % 10 == 0):
        print('%d iterations completed;' % (counter + 1))
end_time = time.time()

my_face_gray_blurred = np.delete(my_face_gray_zeropadded, my_face_gray_zeropadded.shape[0]-1, 0)
my_face_gray_blurred = np.delete(my_face_gray_blurred, 0, 0)
my_face_gray_blurred = np.delete(my_face_gray_blurred, my_face_gray_zeropadded.shape[1]-1, 1)
my_face_gray_blurred = np.delete(my_face_gray_blurred, 0, 1)
plt.imshow(my_face_gray_blurred, cmap='gray')
plt.show()

print('The image-blurring process took %f seconds.' % (end_time - start_time))
Begin blurring my gray face...
0 iterations completed;
10 iterations completed;
20 iterations completed;
30 iterations completed;
40 iterations completed;
50 iterations completed;
60 iterations completed;
70 iterations completed;
80 iterations completed;
90 iterations completed;
100 iterations completed;
The image-blurring process took 348.871673 seconds.