Question

In: Computer Science

The main function creates 5 pixels with random red, green, and blue values. Complete the id_color...

The main function creates 5 pixels with random red, green, and blue values. Complete the id_color function to return “red” if the red value is greater than the green and blue, “green” if the green value is greater than the red and blue, and “blue” if the blue value is greater than the red and green. If there is no clear maximum value (for example, if the red and green values are the same) return None. Do not change the main function.

import image
import random

def id_color(p):
'''Returns the dominant color of pixel p'''
pass
#Your code here to determine the dominant color and return a string identifying it
#Hint: get the red, green & blue values and use an if statement to determine which has the highest value

def main():
'''Controls the program'''
for i in range(5): #Loop 5 times
r = random.randrange(0, 256)
g = random.randrange(0, 256)
b = random.randrange(0, 256)
print(r, g, b) #Show the pixel red, green & blue values
new_pixel = image.Pixel(r, g, b)
print(id_color(new_pixel))

main() #Run the program

Solutions

Expert Solution

So you create a pixel inside the main function five times (using for loop) and you call the get_color(new_pixel) function to get the highest value i.e the dominant color out of Red, Green and Blue that every pixel has, so you call this function five times inside the for loop so you should get five strings as return values from the get_color function and you print it in the main function.

Upvote if you find this useful!

You can complete the get_color(new_pixel) function this way:

import PIL as Image

def id_color(p):
r,g,b=p.getpixel((1,1))
if r>g and r>b:
return "red"
elif g>b and g>r:
return "green"
elif b>r and b>g:
return "blue"
elif r==g and r>b:
return None
elif r==b and r>g:
return None
elif g==b and g>r:
return None

So the basic process is:

1. Get the RGB values of p i.e pixel value passed from main function using getpixel() function from PIL

2. Check if Red is greater than blue and green, if yes then return "red"

3.Check if Green is greater than blue and red, if yes return "green"

4. Check if Blue is greater than red and green, if yes return "blue"

5. Check if any two values are equal if yes then check if they are the largest if yes then return None


Related Solutions

There are three types of balls in a box: 5 red, 3 blue and 2 green....
There are three types of balls in a box: 5 red, 3 blue and 2 green. You draw 3 balls at once (without replacement) from this box and record: Y1=the # of red balls, Y2=the # of blue balls that you drew. Find the joint probability distribution of Y1, Y2, by first writing the possible values for y1, y2 in rows and columns and then filling in the probabilities within this table. Then check that the sum of the entries...
An urn contains 4 red, 5 blue, 2 yellow, and 9 green balls. A group of...
An urn contains 4 red, 5 blue, 2 yellow, and 9 green balls. A group of 5 balls is selected at random without replacement. What is the probability that the sample contains: a. atleast one of each color? b. at most 3 green balls?
A bag contains 4 Blue, 5 red and 3 green balls. Debolina is asked to pick...
A bag contains 4 Blue, 5 red and 3 green balls. Debolina is asked to pick up three balls at random, one by one without replacement from that bag. If the balls picked up are of same colour then Debolina will get Rs. 7000, if the balls picked up are of different colour, then she will get Rs. 9000, but otherwise she has to pay Rs. 4000. a) Find the expected gain of Debolina if she plays the game. b)...
Consider the following dataset: x1 Yellow Yellow Green Green Red Red Green Red Yellow x2 5...
Consider the following dataset: x1 Yellow Yellow Green Green Red Red Green Red Yellow x2 5 2 10 4 3 7 2 5 4 y 1 3 7 5 10 18 4 8 3 (a) (3pts) Split the data into a train and test set where the test set contains observations 1 and 7. Answer this by writing down the two datasets. (b) (2pts) Use Rstudio to estimate a model (which uses both x1 and x2 as predictor variables) using...
A box contains 5 green marbles, 8 blue marbles, and 8 red marbles. Three marbles are...
A box contains 5 green marbles, 8 blue marbles, and 8 red marbles. Three marbles are selected at random from the box, one at a time, without replacement. Find the probability that the first two marbles selected are not red, and the last marble is red.
Twelve chairs are to be painted. The available colors are Red, Blue, Green, and Yellow, and...
Twelve chairs are to be painted. The available colors are Red, Blue, Green, and Yellow, and every chair must be painted with one color. Assuming that all coloring schemes are equally likely, what is the probability that one color is not used and there are at least two chairs of every other color ? (The answer is 0.25) Can anyone explain to me why ?
An urn contains colored balls;5 red balls, 8 green balls, and 10 blue balls. Suppose ...
An urn contains colored balls;5 red balls, 8 green balls, and 10 blue balls. Suppose  If the 3 balls are drawn one after another without replacement, what is the probability that the colors observed will be Red, Green, Blue in this order?  If the three balls are drawn simultaneously from the urn (without replacement), what is the probability that the selected balls will be all different?
A box contains 5 red marbles, 9 blue marbles, and 11 green marbles. We draw two...
A box contains 5 red marbles, 9 blue marbles, and 11 green marbles. We draw two marbles randomly without replacement. Which ones are correct? The probability that the first one is red equals . The probability that the first one is red and the second one is blue equals Given that the first one is NOT blue, the probability that the second marble is blue equals The probability that neither is green equals
1) An urn contains 10 balls, 2 red, 5 blue, and 3 green balls. Take out...
1) An urn contains 10 balls, 2 red, 5 blue, and 3 green balls. Take out 3 balls at a random, without replacement. You win $2 for each green ball you select and lose $3 for each red ball you select. Let the random variable X denote the amount you win, determine the probability mass function of X. 2) Each of the 60 students in a class belongs to exactly one of the three groups A,B, or C. The membership...
13) A box contains 5 green marbles, 6 blue marbles, and 8 red marbles. Three marbles...
13) A box contains 5 green marbles, 6 blue marbles, and 8 red marbles. Three marbles are selected at random from the box, one at a time, without replacement. Find the probability that the first two marbles selected are not red, and the last marble is red. Round your answer to four decimal places.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT