< Back

Lab 15: Compound Images


Practice Problem 1

Write a function multiply_letters(a_phrase, number_of_times), which takes a string and an integer as a parameter. Your function should accumulate the characters from a_phrase, number_of_times times.

Example

>>> multiply_letters("INQ 241A", 2)
IINNQQ  224411AA
>>> multiply_letters("INQ 241A", 3)
IIINNNQQQ   222444111AAA

Hint

All lines that belong to the function must be indented one tab from the left margin. This signifies that you are including that line of code with the function.

You need to use the accumulator pattern here. The accumulator pattern always begins by setting the accumulator variable before your for loop. In this case, you are accumulating strings, so your accumulator should start at "".

Inside of your for loop, you need to accumulate. Accumulation is usually of the form accumulator = accumulator + some_value, but the operation can sometimes change.

Don't forget, you can multiply a string by an integer.


Practice Problem 2

Write a function sum_integers(maximum_integer), which takes an integer as a parameter. This function should print the sum of all of the numbers starting at 0, up to but not including maximum_integer.

Example

>>> sum_integers(10)
45
>>> sum_integers(5)
10

Hint

This activities is also going to use a for loop. In this case, you don't have a string you are executing on. However, you do know the range of integers you want to iterate over.

The range function takes several parameters. However, the one you want to use here is range(beginning, end), which takes the value you wish to start generating integers, and the last value you wish to generate. This is the second value you specify for your for loop.


Green Screen

In a file called green_screen.py, write a function called green_screen(foreground_picture, background_picture). This function should take 2 parameters: the picture you are going replace the green in, and the picture you are going to fetch the background pixels from. Your function should display a picture where all of the green pixels from foreground_picture with the corresponding pixel from background_picture.

Example

Parameter Images After

Hint

You need to iterate over the pixels of your image for this assignment. However, you need to know the x and y locations of each pixel. You can either use the nested for loop to get x and y coordinates, or you can use the getX(pixel) and getY(pixel) functions to get your coordinates.

You can use the distance function to determine if two pixels have a color that is similar. If a pixel's color is stored in a variable called pixel_color, distance(green_screen_color, pixel_color) will return a floating point value representing how far apart the colors are. Use this function to determine if the pixel chosen is close enough to green.

The setPixel(a_picture, x, y) function can be used to get a specific pixel from an image, by specifying the x and y coordinates.

if the pixel is close enough to green, then set the color of the pixel to that of the corresponding pixel from the background_image.