To convert an image to grayscale using python, a solution is to use PIL example:
from PIL import Image
img = Image.open('lena.png').convert('LA')
img.save('greyscale.png')
Note: the conversion to grayscale is not unique see l'article de wikipedia's article). It is also possible to convert an image to grayscale and change the relative weights on RGB colors, example:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.299, 0.587, 0.144])
img = mpimg.imread('lena.png')
gray = rgb2gray(img)
plt.imshow(gray, cmap = plt.get_cmap('gray'))
plt.savefig('lena_greyscale.png')
plt.show()
References
Links | Site |
---|---|
convert rgb image to grayscale in python | stackoverflow |
PIL | PIL |
Grayscale | Wikipedia |
Image manipulation and processing using Numpy and Scipy | Scipy Doc |
python - RGB matrix of an image | stackoverflow |
Display image as grayscale using matplotlib | stackoverflow |