Exercises for Lesson 12

Back to Lesson 12

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)  (0, 255, 255)

(iii) (120, 0, 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

Back to Lesson 12

Exercise 2: Creating an image

Write a function that generates (and returns) a 400x200 image with one randomly-chosen color per row. To do this, fill in createRandomRowImage in the program below.

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
    """
    # Generate a blank 400x200 image (not starting from a file)
    image = Image(Point(0,0), 400, 200)

    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()

Back to Lesson 12

Exercise 3: Getting image colors

Complete the following program to report 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 # TODO: replace with your code

def main():
    # Load the image
    image = Image(Point(0,0), "cheddar_sleep.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()

When you run this and click on an image, you should get output like the following:

Click (x=373, y=260): r=102, g=85, b=51.

Back to Lesson 12

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 *

def getGrayscaleColor(r, g, b):
    """
    Converts the pixel to grayscale using the formula
       Y = 0.2126*r + 0.7152*g + 0.0722*b.

    r: the red channel (int in the range 0 to 255)
    g: the green channel (int in the range 0 to 255)
    b: the blue channel (int in the range 0 to 255)
    returns: the result of color_rgb (int in the range 0 to 255)
    """
    return None # replace with your code

def createGrayscaleImage(origImage):
    """
    Creates a copy of the original image, and colors it grayscale.

    origImage: the original image (graphics.py Image object)
    returns: the grayscale image
    """
    return None # replace with your code

def main():
    # Load the original image
    image = Image(Point(0,0), "cheddar_sleep.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)

    # Exit after the user clicks
    win.getMouse()
    win.close()

if __name__ == "__main__":
    main()

Back to Lesson 12