How can I convert an RGB image into grayscale in Python?

前端 未结 12 1248
Happy的楠姐
Happy的楠姐 2020-11-22 04:43

I\'m trying to use matplotlib to read in an RGB image and convert it to grayscale.

In matlab I use this:

img = rgb2gray(imread(\'image.p         


        
12条回答
  •  孤城傲影
    2020-11-22 05:06

    You can also use scikit-image, which provides some functions to convert an image in ndarray, like rgb2gray.

    from skimage import color
    from skimage import io
    
    img = color.rgb2gray(io.imread('image.png'))
    

    Notes: The weights used in this conversion are calibrated for contemporary CRT phosphors: Y = 0.2125 R + 0.7154 G + 0.0721 B

    Alternatively, you can read image in grayscale by:

    from skimage import io
    img = io.imread('image.png', as_gray=True)
    

提交回复
热议问题