1import numpy as np
2import matplotlib.pyplot as plt
3import matplotlib.image as mpimg
4
5def rgb2gray(rgb):
6 return np.dot(rgb[...,:3], [0.299, 0.587, 0.144])
7
8img = mpimg.imread('img.png')
9
10gray = rgb2gray(img)
11
12plt.imshow(gray, cmap='gray')
13
14plt.savefig('greyscale.png')
15plt.show()
16