Exercises for Lesson 13
Exercise 1: Colors
Part a: Guessing colors
For each (r,g,b) triple below, what color do you expect to see?
(i) (255, 0, 0)
(ii) (255, 0, 255)
(iii) (0, 120, 120)
(iv) (255, 128, 0)
Part b: Creating colors
For each color name below, give an (r,g,b) triple that would be close to that color.
(i) yellow
(ii) medium gray
(iii) salmon
(iv) forest green
Exercise 2: Getting image colors
Write a program that reports the pixel color where a user clicked on an image, until the user has clicked ten times.
from graphics import *
def reportClick(image, p):
"""
Prints out the location and color where the user clicked on the image.
image: an Image
p: the Point where the user clicked
"""
pass # replace with your code
def main():
# Load the image
image = Image(Point(0,0), "stripes.gif")
width = image.getWidth()
height = image.getHeight()
# Create the window
win = GraphWin("Image Processing", width, height)
# Draw the image
image.move(width/2, height/2)
image.draw(win)
# Report the color and location of ten user clicks
for i in range(10):
p = win.getMouse()
reportClick(image, p)
# Exit after one more click
win.getMouse()
win.close()
if __name__ == "__main__":
main()
Exercise 3: Creating an image
Write a function that generates (and returns) a 400x200 image with one randomly-chosen color per row.
from graphics import *
import random
def chooseColor():
"""
Randomly chooses a color
return: the color (using color_rgb)
"""
r = random.randint(0,255)
g = random.randint(0,255)
b = random.randint(0,255)
return color_rgb(r, g, b)
def createRandomRowImage():
"""
Creates a 400x200 image and assigns each row a random color.
returns: the random-row image
"""
return None # replace with your code
def main():
# Create the random-row image
image = createRandomRowImage()
width = image.getWidth()
height = image.getHeight()
# Create the window
win = GraphWin("Image Processing", width, height)
# Draw the image
image.move(width/2, height/2)
image.draw(win)
if __name__ == "__main__":
main()
Exercise 4: Grayscale based on human perception
We can choose the grayscale value for a pixel using the following calculation:
Y = 0.2126 * R + 0.7152 * G + 0.0722 * B
Fill in the following program. It should compute Y for each pixel, and color that pixel (Y,Y,Y) instead.
from graphics import *
import random
def getGrayscaleColor(r, g, b):
"""
Converts the pixel to grayscale using the formula
Y = 0.2126*r + 0.7152*g + 0.0722*b.
returns: the result of color_rgb
"""
return None # replace with your code
def createGrayscaleImage(origImage):
"""
Creates a copy of the original image, and colors it grayscale.
returns: the grayscale image
"""
return None # replace with your code
def main():
# Load the original image
image = Image(Point(0,0), "squash.gif")
width = image.getWidth()
height = image.getHeight()
# Create the window to display both images
win = GraphWin("Image Processing", width*2, height)
# Draw the original image
image.move(width/2, height/2)
image.draw(win)
# Create the grayscale image
grayscaleImage = createGrayscaleImage(image)
grayscaleImage.move(width, 0)
grayscaleImage.draw(win)
if __name__ == "__main__":
main()