PIXELBANKv9.1.0
Menu

RGB to Grayscale Conversion

Implement a function to convert a list of RGB pixels to their corresponding grayscale values. This process involves transforming color spaces, where each pixel's luminance is calculated based on its red, green, and blue components.

The human visual system is more sensitive to certain colors, which is reflected in the weighted sum used for conversion. The sensitivity to green is highest, followed by red, and then blue, which is represented by the coefficients in the conversion formula.

To perform the conversion, follow these steps:

  1. Extract the R, G, and B values from each pixel.
  2. Apply the weighted sum to calculate the luminance.
  3. Round the result to obtain the grayscale value.
Y=round(0.299â‹…R+0.587â‹…G+0.114â‹…B)Y = \text{round}(0.299 \cdot R + 0.587 \cdot G + 0.114 \cdot B)

This technique is widely used in image processing applications.

Example:

Input:
pixels = [[255, 0, 0], [0, 255, 0], [0, 0, 255]]
Output:
[76, 150, 29]
Reasoning:
  • We apply the luminance formula to each RGB pixel:
    • For [255, 0, 0]: Y=round(0.299â‹…255+0.587â‹…0+0.114â‹…0)=round(76.245)=76Y = \text{round}(0.299 \cdot 255 + 0.587 \cdot 0 + 0.114 \cdot 0) = \text{round}(76.245) = 76
    • For [0, 255, 0]: Y=round(0.299â‹…0+0.587â‹…255+0.114â‹…0)=round(149.535)=150Y = \text{round}(0.299 \cdot 0 + 0.587 \cdot 255 + 0.114 \cdot 0) = \text{round}(149.535) = 150
    • For [0, 0, 255]: Y=round(0.299â‹…0+0.587â‹…0+0.114â‹…255)=round(28.86)=29Y = \text{round}(0.299 \cdot 0 + 0.587 \cdot 0 + 0.114 \cdot 255) = \text{round}(28.86) = 29
  • The final output is a list of these calculated grayscale values: [76, 150, 29]

Constraints:

  • Each pixel is [R, G, B] with values in [0, 255]
  • Return list of integers (use round())
  • Pure Python, no libraries needed
solution.py

Test Results

0/0
Run code to see test results.