Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.png')); 

In the matplotlib tutorial they don't cover it. They just read in the image

import matplotlib.image as mpimg img = mpimg.imread('image.png') 

and then they slice the array, but that's not the same thing as converting RGB to grayscale from what I understand.

lum_img = img[:,:,0] 

I find it hard to believe that numpy or matplotlib doesn't have a built-in function to convert from rgb to gray. Isn't this a common operation in image processing?

I wrote a very simple function that works with the image imported using imread in 5 minutes. It's horribly inefficient, but that's why I was hoping for a professional implementation built-in.

Sebastian has improved my function, but I'm still hoping to find the built-in one.

matlab's (NTSC/PAL) implementation:

import numpy as np  def rgb2gray(rgb):      r, g, b = rgb[:,:,0], rgb[:,:,1], rgb[:,:,2]     gray = 0.2989 * r + 0.5870 * g + 0.1140 * b      return gray 
like image 842
waspinator Avatar asked Aug 30 '12 16:08

waspinator


People also ask

How do I convert an image from RGB to grayscale in Python?

Convert an Image to Grayscale in Python Using the Conversion Formula and the Matplotlib Library. We can also convert an image to grayscale using the standard RGB to grayscale conversion formula that is imgGray = 0.2989 * R + 0.5870 * G + 0.1140 * B .

How do I convert a RGB image to grayscale?

You just have to take the average of three colors. Since its an RGB image, so it means that you have add r with g with b and then divide it by 3 to get your desired grayscale image. Its done in this way.

How do I convert RGB image to grayscale in Python OpenCV?

Step 1: Import OpenCV. Step 2: Read the original image using imread(). Step 3: Convert to grayscale using cv2. cvtcolor() function.

How do I convert RGB image to grayscale in Python using PIL?

To convert PIL Image to Grayscale in Python, use the ImageOps. grayscale() method. PIL module provides ImageOps class, which provides various methods that can help us to modify the image.


1 Answers

How about doing it with Pillow:

from PIL import Image img = Image.open('image.png').convert('L') img.save('greyscale.png') 

If an alpha (transparency) channel is present in the input image and should be preserved, use mode LA:

img = Image.open('image.png').convert('LA') 

Using matplotlib and the formula

Y' = 0.2989 R + 0.5870 G + 0.1140 B  

you could do:

import numpy as np import matplotlib.pyplot as plt import matplotlib.image as mpimg  def rgb2gray(rgb):     return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])  img = mpimg.imread('image.png')      gray = rgb2gray(img)     plt.imshow(gray, cmap=plt.get_cmap('gray'), vmin=0, vmax=1) plt.show() 
like image 148
unutbu Avatar answered Oct 11 '22 21:10

unutbu