Question

Create a python program that takes in an image from the user and puts the image...

Create a python program that takes in an image from the user and puts the image into matrices of red, green, and blue. Then use the equation Y = 0.2126R + 0.7125G + 0.0722B to convert the image to grayscale and output the grayscale image. You must only use the libraries numpy, matplotlib.pyplot, and math. You may not use pillow, openCV, sciPy, etc.

Homework Answers

Answer #1

Python code:

# Import necessary libraries
import numpy as np
import matplotlib
import matplotlib.pyplot as pyplot

# Colored image input
img = matplotlib.image.imread("img.jpg")

# Print the RGB matrix
print("The matrix for the colored image is:")
print(np.array(img))

# Display the colored image
print("The colored image is: ")
pyplot.imshow(img)
pyplot.show()

# Define the RGB weights according to the equation (Y = 0.2126R + 0.7125G + 0.0722B)
rgb_weights = [0.2126, 0.7125, 0.0722]

# Convert into grayscale and display
print("The grayscale image is: ")
grayscale_image = np.dot(img[...,:3], rgb_weights)
pyplot.imshow(grayscale_image, cmap=pyplot.get_cmap("gray"))
pyplot.show()

Output:

The matrix for the colored image is:
[[[ 78 106  48]
  [ 78 106  47]
  [ 78 106  47]
  ...
  [152 180  96]
  [152 180  96]
  [152 180  96]]

 [[ 78 106  48]
  [ 78 106  47]
  [ 78 106  47]
  ...
  [152 180  96]
  [152 180  96]
  [152 180  96]]

 [[ 78 106  48]
  [ 78 106  47]
  [ 78 106  47]
  ...
  [153 181  97]
  [153 181  97]
  [153 181  97]]

 ...

 [[ 93 123  61]
  [ 93 123  61]
  [ 93 123  61]
  ...
  [160 188 104]
  [160 188 104]
  [160 188 104]]

 [[ 93 123  61]
  [ 93 123  61]
  [ 93 123  61]
  ...
  [160 188 104]
  [160 188 104]
  [160 188 104]]

 [[ 93 123  61]
  [ 93 123  61]
  [ 93 123  61]
  ...
  [160 188 104]
  [160 188 104]
  [160 188 104]]]

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a program that creates an image of green and white horizontal stripes. Your program should...
Write a program that creates an image of green and white horizontal stripes. Your program should ask the user for the size of your image, the name of the output file, and create a .png file of stripes. For example, if the user enters 10, your program should create a 10x10 image, alternating between green and white stripes. A sample run of the program: Enter the size: 10 Enter output file: 10stripes.png Another sample run of the program: Enter the...
step by step in python please The program will prompt the user as to whether you...
step by step in python please The program will prompt the user as to whether you want to convert from Celsius to Fahrenheit or from Fahrenheit to Celsius Write it so each conversion is contained within its own function (i.e., one function to do the math in one direction, a second to do the math in the other direction) These two functions should just have the input temperature as a parameter and return the output temperature in the other units....
create a basic python program that converts binary to number (using if statements etc) Asking user...
create a basic python program that converts binary to number (using if statements etc) Asking user for input of binary number converting/calculating printing
Write a program in python to display all the consonant in the user input. E.g. user...
Write a program in python to display all the consonant in the user input. E.g. user input: Hello Good Day to you. Output: consonant H = 1 consonant l = 2 consonant G = 1   consonant d = 1 consonant D = 1 etc
Using Python, students will use variables, input, and printing to create a Mad Lib. The program...
Using Python, students will use variables, input, and printing to create a Mad Lib. The program will print out the title of the Mad Libs story, as well as a short explanation of game play: The program should then prompt the user to enter in nouns, verbs, adjectives, proper nouns, and adverbs: Enter a proper noun: Enter a place: Enter another place: Enter an adverb: Enter a noun: Enter an adjective: Enter an adverb: Enter a verb: Enter a place:...
Create a Python program that: Allows the user to enter a phrase or sentence. The program...
Create a Python program that: Allows the user to enter a phrase or sentence. The program should then take the phrase or sentence entered Separate out the individual words entered Each individual word should then be added to a list After all of the words have been place in a list Sort the contents of the list Display the contents of the sorted list with each individual word displayed on a separate line Display a message to the user indicating...
Write a Python program that plays a number guessing game with a human user. The human...
Write a Python program that plays a number guessing game with a human user. The human user will think of a number between 1 and 100, inclusive. Then the program has to guess what the user entered. keep track of the number of interaction it takes for the computer to guess the number. sample run: enter number to be guessed:88 output: you entered 88, and it took the program 3 iterations to guess.
In Python You must create a flowchart, and its corresponding Python program, to solve the following...
In Python You must create a flowchart, and its corresponding Python program, to solve the following problem: 1. Using a repetition structure, evaluate the factorial of a positive whole number n: n! = n · (n - 1) · (n - 2) · ... · 1, where n >=1 Your program must take into account the cases in which the user enters an incorrect number and provide an error message for such cases.
Write a Python program which calculates the average of the numbers entered by the user through...
Write a Python program which calculates the average of the numbers entered by the user through the keyboard. Use an interactive loop and ask the user at each iteration if he/she wants to enter more numbers. At the end dispay the average on the screen. Using built-in library functions for finding average is not allowed. Sample output of the program: Enter a number > 23 More numbers (yes or no)? y Enter a number > 4 Do you have more...
Python. create a function that takes a string as a parameter and prints out the following...
Python. create a function that takes a string as a parameter and prints out the following details as the example output below. If the string is "Hello, my name is Starling, Clarice. My, my, my, nice to meet you." The output would be: Unique words: 10 Number of commas: 5 Number of periods: 2 Number of sentences: 2 (HINT: Use the following string methods: split, lower, count, replace, format, and use the following functions: set, len. You may need to...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT