python please File: testgrayscale.py Tests a function for converting a color ima
ID: 3905493 • Letter: P
Question
python please
File: testgrayscale.py Tests a function for converting a color image to grayscale from images import Image def grayscalefimage) """Converts the argunent image to grayscale, tor ornsinuange( inage.getPixel (?.'yi for x in xrange (image.getWidthO getPixel return tuple of RGB values weight each component for human {r, r- g, b) int(r -image.getpixel(x, ?0.299) y) # perception lum = r + g + b inage.setPixel(x, y. Lum, lum, lum)) def main(filename "smokey.gif") image Image(filename print "close the image window to continue, image.araw) grayscaletimage) print "close the image windo to quit. image.draw) main() Write the following image processing programs: Copy the above testgrayscale.py program and modify it to perform a simplified gray-scaling calculation. For each pixel, calculate the average of the r. g. b values of the color image, and set all three r, g. b values to this average. Use the same main program to test your new function. Write a setColor function that expects two arguements: an image and an RGB tuple containing three integers. The setColor function should set every pixel in the image to the RGB tuple color. Test the setColor function from an interactive main program that creates a blank image that is 200 pixels wide by 100 pixels tall, allows the user to enter three integers for the RGB values, prints the prompt "Close the image window to continue", calls setColor with the image and the user specified color tuple, and draws the image . .Explanation / Answer
Please note at line 41 I am using PIL Image class for creating blank image you can use your own Image class accordingly.
(python 2)
from images import Image
# Question 2
def setColor(image, rgb):
"""
Sets all the pixels in the image with the passed RGB values.
Parameters
----------
image : Image
rgb : Tuple
"""
for y in xrange(image.getHeight()):
for x in xrange(image.getWidth()):
# Setting the pixel with the rgb values
image.setPixel(x, y, rgb)
def grayscale(image):
""" Converts the argument image to greyscale."""
for y in xrange(image.getHeight()):
for x in xrange(image.getWidth()):
(r, g, b) = image.getPixel(x, y)
# New code as per Question 1
avg_val = (r + g + b) / 3
image.setPixel(x, y, (avg_val, avg_val, avg_val))
# Un/Comment accordingly for running the main method
def main(filename='smokey.gif'):
image = Image(filename)
print 'Close the image window to continue.'
image.draw()
grayscale(image)
print 'Close the image window to quit.'
image.draw()
# Un/Comment accordingly for running the main method to test the setColor method (Question 2)
def main():
# NOTE: Please change the below Image initialization as per the Image class you
# have created and importing from images module.
# I'm using the PIL module's Image class for creating a blank image with width 200 and height 100
from PIL import Image
image = Image.new('RGB', (200, 100)) # Creating blank image
rgb = tuple(map(int, raw_input('Enter the RGB values: ').split(' '))) # Taking user's rgb values like 10 20 40
print 'Close the image window to continue.'
setColor(image, rgb) # Calling the setColor method
image.draw() # Drawing the image
main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.