API details

A simple API for converting an RGB to a grayscale image.

source

rgb2gray

 rgb2gray (img)

Convert RGB image to grayscale.

Here is an example of conversion…

import matplotlib.pyplot as plt
from io import BytesIO
import requests
red = Image.new('RGB', (10, 10), color = (255,0,0))
rgb2gray(red)
array([[76, 76, 76, 76, 76, 76, 76, 76, 76, 76],
       [76, 76, 76, 76, 76, 76, 76, 76, 76, 76],
       [76, 76, 76, 76, 76, 76, 76, 76, 76, 76],
       [76, 76, 76, 76, 76, 76, 76, 76, 76, 76],
       [76, 76, 76, 76, 76, 76, 76, 76, 76, 76],
       [76, 76, 76, 76, 76, 76, 76, 76, 76, 76],
       [76, 76, 76, 76, 76, 76, 76, 76, 76, 76],
       [76, 76, 76, 76, 76, 76, 76, 76, 76, 76],
       [76, 76, 76, 76, 76, 76, 76, 76, 76, 76],
       [76, 76, 76, 76, 76, 76, 76, 76, 76, 76]], dtype=uint8)
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(10,10))
axes[0].imshow(red)
axes[1].imshow(rgb2gray(red), cmap='gray')
plt.show();

img = Image.open('images/ds.jpg')
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(15,10))

axes[0].imshow(img)
axes[1].imshow(rgb2gray(img), cmap='gray')
plt.show();