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

前端 未结 12 1118
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:00

    Use img.Convert(), supports “L”, “RGB” and “CMYK.” mode

    import numpy as np
    from PIL import Image
    
    img = Image.open("IMG/center_2018_02_03_00_34_32_784.jpg")
    img.convert('L')
    
    print np.array(img)
    

    Output:

    [[135 123 134 ...,  30   3  14]
     [137 130 137 ...,   9  20  13]
     [170 177 183 ...,  14  10 250]
     ..., 
     [112  99  91 ...,  90  88  80]
     [ 95 103 111 ..., 102  85 103]
     [112  96  86 ..., 182 148 114]]
    

提交回复
热议问题