Question

In: Computer Science

Write a Python script that takes an input image and output's the name of the dominant...

Write a Python script that takes an input image and output's the name of the dominant color in that image(i.e. red, green, blue).  

Solutions

Expert Solution

import matplotlib.image as img

import matplotlib.pyplot as plt

from scipy.cluster.vq import whiten

from scipy.cluster.vq import kmeans

import pandas as pd

iron_image = img.imread('iron.jpg') //created a random img

r = []

g = []

b = []

for row in iron_image:

    for temp_r, temp_g, temp_b, temp in row:

        r.append(temp_r)

        g.append(temp_g)

        b.append(temp_b)

  

iron_df = pd.DataFrame({'red' : r,

                          'green' : g,

                          'blue' : b})

iron_df['scaled_color_red'] = whiten(iron_df['red'])

iron_df['scaled_color_blue'] = whiten(iron_df['blue'])

iron_df['scaled_color_green'] = whiten(iron_df['green'])

cluster_centers, _ = kmeans(iron_df[['scaled_color_red',

                                    'scaled_color_blue',

                                    'scaled_color_green']], 3)

dominant_colors = []

red_std, green_std, blue_std = iron_df[['red',

                                          'green',

                                          'blue']].std()

for cluster_center in cluster_centers:

    red_scaled, green_scaled, blue_scaled = cluster_center

    dominant_colors.append((

        red_scaled * red_std / 255,

        green_scaled * green_std / 255,

        blue_scaled * blue_std / 255

    ))

plt.imshow([dominant_colors])

plt.show()

i hope you will like my response,  and please don't forget to give thumbs up!!

if you have any query, do share in comment box!!

Stay safe and healthy!!

Thank you!!!


Related Solutions

Write a python function image compress() that takes one argument called filename, which is the name...
Write a python function image compress() that takes one argument called filename, which is the name of a file that contains a N × N (N-pixel by N-pixel) “grayscale bitmap image”. A “grayscale bitmap image” is an image of the following form where every pixel contains a grayscale color value between 0 − 255 (inclusive). Colour value 0 means that pixel should appear completely black and color value 255means completely white. Any other value in between stands for different shades...
Task 2.5: Write a script that will ask the user for to input a file name...
Task 2.5: Write a script that will ask the user for to input a file name and then create the file and echo to the screen that the file name inputted had been created 1. Open a new file script creafile.sh using vi editor # vi creafile.sh 2. Type the following lines #!/bin/bash echo ‘enter a file name: ‘ read FILENAME touch $FILENAME echo “$FILENAME has been created” 3. Add the execute permission 4. Run the script #./creafile.sh 5. Enter...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x // 2 Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string....
Write a program in python that takes a date as input and outputs the date's season....
Write a program in python that takes a date as input and outputs the date's season. The input is a string to represent the month and an int to represent the day. Ex: If the input is: April 11 the output is: Spring In addition, check if the string and int are valid (an actual month and day). Ex: If the input is: Blue 65 the output is: Invalid The dates for each season are: Spring: March 20 - June...
USING PYTHON, write a function that takes a list of integers as input and returns a...
USING PYTHON, write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2]. DO NOT use any special or built in functions like append, reverse etc.
Write a Python program that takes as input two numbers, the height, and width of a...
Write a Python program that takes as input two numbers, the height, and width of a rectangle. It then prints a rectangle with height lines and width characters in each line. The rectangle should contain information regarding its dimensions and area inside (centered vertically but not horizontally). If any part of the information (including two stars on each end and a space before and after the line) does not fit in the rectangle, then print the complete information after the...
write a python function that takes a number as input argument, and that tries to determine...
write a python function that takes a number as input argument, and that tries to determine two other integers, root and pwr, such that root**pwr is equal to the integer passed as an argument to the function. Consider that pwr > 1. The function should return the values of root and pwr, as a string in the form root**pwr. For example, when the passed argument is 8, the function should return the string ‘2**3’. When the passed input argument does...
Write a Python script that will collect 5 grades (float) from the student via input and...
Write a Python script that will collect 5 grades (float) from the student via input and obtain the average. requirements: a. Collect 5 floating grades from student b. Obtain the average of the grades
Write a bash shell script that takes exactly one argument, a file name. If the number...
Write a bash shell script that takes exactly one argument, a file name. If the number of arguments is more or less than one, print a usage message. If the argument is not a file name, print another message. For the given file, print on the screen its content.
Java Code: Write an application that takes in user input. (Name, Age, and Salary) ------ Write...
Java Code: Write an application that takes in user input. (Name, Age, and Salary) ------ Write an application that includes a constructor, user input, and operators.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT