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:
- Extract the R, G, and B values from each pixel.
- Apply the weighted sum to calculate the luminance.
- Round the result to obtain the grayscale value.
This technique is widely used in image processing applications.
Example
pixels = [[255, 0, 0], [0, 255, 0], [0, 0, 255]]
[76, 150, 29]
- Input:
pixelsis a list of pixels, each a triple of channel intensities from (off) to (full). The sample holds pure red, pure green and pure blue, each at full strength. - Math: each pixel collapses to one luminance number , rounded to the nearest integer. With only one channel lit, is just that channel's weight times :
- red:
- green:
- blue:
- Output: one integer per pixel, in the same order as the input, giving
[76, 150, 29]. - Notice that three equally bright colours get very different grays: green looks brightest to the eye, blue darkest. Because the weights add up to , white maps to and black to , so the result stays in .
Constraints
- Each pixel is [R, G, B] with values in [0, 255]
- Return list of integers (use round())
- Pure Python, no libraries needed