How to convert an image to grayscale using python ?

February 19, 2019    /    Viewed: 84133    /    Comments: 0    /    Edit

To convert an image to grayscale using python, a solution is to use PIL example:

How to convert an image to grayscale using python ?

from PIL import Image

img ='lena.png').convert('LA')'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[...,:3], [0.299, 0.587, 0.144])

img = mpimg.imread('lena.png')

gray = rgb2gray(img)

plt.imshow(gray, cmap = plt.get_cmap('gray'))



Links Site
convert rgb image to grayscale in python stackoverflow
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

Card image cap

Hi, I am Ben.

I have developed this web site from scratch with Django to share with everyone my notes. If you have any ideas or suggestions to improve the site, let me know ! (you can contact me using the form in the welcome page). Thanks!