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

前端 未结 12 1247
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 04:59

    I came to this question via Google, searching for a way to convert an already loaded image to grayscale.

    Here is a way to do it with SciPy:

    import scipy.misc
    import scipy.ndimage
    
    # Load an example image
    # Use scipy.ndimage.imread(file_name, mode='L') if you have your own
    img = scipy.misc.face()
    
    # Convert the image
    R = img[:, :, 0]
    G = img[:, :, 1]
    B = img[:, :, 2]
    img_gray = R * 299. / 1000 + G * 587. / 1000 + B * 114. / 1000
    
    # Show the image
    scipy.misc.imshow(img_gray)
    

提交回复
热议问题