-1

If the problem I am given is a nested tuple with rgb pixels, how do I convert that to grayscale and return a tuple with the grayscale pixel values. This should all be within one function.

Thanks

I honestly have no where to start since I am beginner programmer so would appreciate any help

petezurich
  • 9,280
  • 9
  • 43
  • 57
yusuf
  • 1

1 Answers1

0

Assuming output entries are integers in range [0; 255] and your initial tuple named image:

from statistics import mean
gray_image = tuple(int(mean(pixel)) for pixel in image)

or more beginner friendly (and assuming pixels just default python integers, not uint8):

gray_image = []  # create empty list

for pixel in image:              
    R, G, B = pixel                # get rgb values
    gray_pixel = (R + G + B) // 3  # averaging to get gray
    gray_image.append(gray_pixel)

gray_image = tuple(gray_image) # turn list to tuple

It's simple averaging, but if there is need to get more technical, please take a look at this answer.

draw
  • 936
  • 3
  • 8