Question

In: Computer Science

Language Python with functions and one main function Write a program that converts a color image...

Language Python

with functions and one main function

Write a program that converts a color image to grayscale. The user supplies the name of a file containing a GIF or PPM image, and the program loads the image and displays the file. At the click of the mouse, the program converts the image to grayscale. The user is then prompted for a file name to store the grayscale image in.

Solutions

Expert Solution

code

solution

//output

//colorful.gif

//output.gif

//copyable code

from tkinter import *
from PIL import ImageTk, Image

#FUNCTION grayscale
def grayscale(event):
#loading image
img_pixels=img.load()
#loop for row and column
for i1 in range(img.size[0]):
for j1 in range(img.size[1]):
#rbg image
rgbsum=sum(img_pixels[i1,j1])
#calculate average
avg1=rgbsum//3
# red, green, blue value
img_pixels[i,j]=(avg1,avg1,avg1)
#update image
photo_img = ImageTk.PhotoImage(img)
imglabel.configure(image=photo_img)
imglabel.image=photo_img

#create a window
root = Tk()
#read a file
input_name=input('please Enter the input file name: ')
#open a image file
img=Image.open(input_name)
#creating a PhotoImage
photoimg = ImageTk.PhotoImage(img)
#create label
imglabel = Label(root, image=photoimg)
imglabel.bind("<Button>",grayscale)
imglabel.grid(row=1, column=1)
#main method
root.mainloop()
#getting output file name
output=input('please Enter the output file name: ')
img.save(output)


Related Solutions

Programming language Python It have to be in Functions with a main function Samuel is a...
Programming language Python It have to be in Functions with a main function Samuel is a math teacher at Hogwarts School of Witchcraft and Wizardry. He loves to give his students multiplication exercises. However, he doesn’t care about the actual operation result but the unit sum of its digits. At Hogwarts School of Witchcraft and Wizardry, they define the unit sum (US) of N as the unit that it is left after doing the sum of all the digits of...
Write an IPO diagram and Python program that has two functions, main and determine_grade. main –...
Write an IPO diagram and Python program that has two functions, main and determine_grade. main – Should accept input of five numeric grades from the user USING A LOOP.   It should then calculate the average numeric grade.    The numeric average should be passed to the determine_grade function. determine_grade – should display the letter grade to the user based on the numeric average:        Greater than 90: A 80-89:                 B 70-79:                 C 60-69:              D Below 60:           F Modularity:...
Program in Python Problem Statement Write a program with the following functions:  wordCount. This function...
Program in Python Problem Statement Write a program with the following functions:  wordCount. This function should accept a string as a parameter and return the number of words contained in the string.  mostFrequentWord. This function accepts a string as a parameter and returns the word that occurs the most frequently in the string.  replaceWord. This function accepts three strings as parameters, let’s call them string1, string2, and string3. It searches string1 for all occurrences of string2. When...
Write a C++ program which consists of several functions besides the main() function. The main() function,...
Write a C++ program which consists of several functions besides the main() function. The main() function, which shall ask for input from the user (ProcessCommand() does this) to compute the following: SumProductDifference and Power. There should be a well designed user interface. A void function called SumProductDifference(int, int, int&, int&, int&), that computes the sum, product, and difference of it two input arguments, and passes the sum, product, and difference by-reference. A value-returning function called Power(int a, int b) that...
C++ Write a program that has two functions. The 1st function is the main function. The...
C++ Write a program that has two functions. The 1st function is the main function. The main function should prompt the user for three inputs: number 1, number 2, and an operator. The main function should call a 2nd function called calculate. The 2nd function should offer the choices of calculating addition, subtraction, multiplication, and division. Use a switch statement to evaluate the operator, then choose the appropriate calculation and return the result to the main function.
Python program please no def, main, functions Given a list of negative integers, write a Python...
Python program please no def, main, functions Given a list of negative integers, write a Python program to display each integer in the list that is evenly divisible by either 5 or 7. Also, print how many of those integers were found. Sample input/output: Enter a negative integer (0 or positive to end): 5 Number of integers evenly divisible by either 5 or 7: 0 Sample input/output: Enter a negative integer (0 or positive to end): -5 -5 is evenly...
Write a program that is capable of detecting the color brown in an image taken by...
Write a program that is capable of detecting the color brown in an image taken by a webcam using OpenCV python code.
Write the following function and provide a program to test it (main and function in one...
Write the following function and provide a program to test it (main and function in one .py) def repeat(string, n, delim) that returns the string string repeated n times, separated by the string delim. For example repeat(“ho”, 3, “,”) returns “ho, ho, ho”. keep it simple.Python
Write a python program. The function protocol implements the functionality of the diagnostic protocol. The main...
Write a python program. The function protocol implements the functionality of the diagnostic protocol. The main idea of this function is to call the functions that you have already implemented in the previous questions. See below for an explanation of exactly what is expected. def protocol(my_symptoms: Tuple[Set], all_patients_symptoms: Dict[str, Tuple[Set]], all_patients_diagnostics: Dict[int, str]) -> str: '''Return the diagnostic for my_symptoms based on the dictionary of patients symptoms (all_patients_symptoms) and the dictionary of patients diagnostics (all_patients_diagnostics). Hint: This function selects the...
Write the following easy Python functions: 1) Write the function named roundDollars(). The function has one...
Write the following easy Python functions: 1) Write the function named roundDollars(). The function has one input, a String named amountStr which consists of a dollar-formatted amount, such as "$ 1,256.86". The returned value is an int representing the number of rounded "dollars" in the amount, (1257 in the sample shown here). You will need to scrub, format and parse the input, then use arithmetic to determine how many rounded dollars the amount contains. roundDollars("$ 1,256.86") → 1257 roundDollars("$ 0.42")...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT