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 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.
Write a Python function that takes as input parameters base_cost (a float) and customer_type and prints...
Write a Python function that takes as input parameters base_cost (a float) and customer_type and prints a message with information about the total amount owed and how much the tip was. As a reminder, the tip amounts are 10%, 15% and 20% for stingy, regular, and generous customers. And the tax amount should be 7%. The total amount is calculated as the sum of two amounts: check_amount = base_cost*1.07 tip_amount = tip_percentage*check_amount To receive full credit, you must use string...
[In Python] Write a program that takes a .txt file as input. This .txt file contains...
[In Python] Write a program that takes a .txt file as input. This .txt file contains 10,000 points (i.e 10,000 lines) with three co-ordinates (x,y,z) each. From this input, use relevant libraries and compute the convex hull. Now, using all the points of the newly constructed convex hull, find the 50 points that are furthest away from each other, hence giving us an evenly distributed set of points.
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns,...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns, as output, the sum of the first n positive odd integers. Your solution should be recursive, and it should not contain any "for" loops or "while" loops.
Text Wrap Problem Write a program in Python that takes an input string and prints it...
Text Wrap Problem Write a program in Python that takes an input string and prints it as multiple lines of text such that no line of text is greater than 13 characters and words are kept whole. For example, the first line of the Gettysburg address: Four score and seven years ago our fathers brought forth upon this continent a new nation, conceived in liberty and dedicated to the proposition that all men are created equal Becomes: Four score and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT