-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrayscale.py
More file actions
34 lines (25 loc) · 948 Bytes
/
grayscale.py
File metadata and controls
34 lines (25 loc) · 948 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import matplotlib.image as mat
import numpy as np
import matplotlib.pyplot as plt
#Here image is a n*3 array
def convert(image,image_name):
avg = 0
width,height = image.shape[:2]
new_image = np.zeros([width,height,3])
for i in range(width):
for j in range(height):
sum = float(image[i][j][0]) + float(image[i][j][1]) + float(image[i][j][2])
avg = (sum/3)/256
new_image[i][j][0] = avg
new_image[i][j][1] = avg
new_image[i][j][2] = avg
gray_image_name = "Gray_" + image_name
mat.imsave(gray_image_name,new_image)
#plt.imshow(new_image)
#plt.show()
print("Your Gray Image stored in the following folder with the name : " , gray_image_name)
return new_image
if __name__ == "__main__":
actual_image = input("Enter Image Name : ")
image = mat.imread(actual_image)
convert(image,actual_image)