< Back

Lab 13: Images


Practice Problem 1

Write a function omit_letters(a_phrase, omitted_letters), which takes a string as a parameter. Your function should list all of the characters out of a_phrase, each on their own line. However, your program should only print the letters not included in the omitted letters string.

Example

>>> omit_letters("INQ 241A", "241")
I
N
Q

A
>>> omit_letters("Trexler", "e")
T
r
x
l
r

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 can use a for loop to go through each character of the string individually. Recall that a for loop on strings is of the form for a_variable in some_string:

We can use an if statement in order to chose whether or not to print a particular letter. You want to print a letter if it is not in the omitted_letters.


Practice Problem 2

Write a function perfect_squares(maximum_sqrt), which takes an integer as a parameter. This function should print all of the perfect squares whose square root is less than maximum_sqrt.

Example

>>> perfect_squares(10)
0
1
4
9
16
25
36
49
64
81
>>> perfect_squares(5)
0
1
4
9
16

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.


Better Hair Replacement

In a file called hair_color.py, write a function called change_hair_to_red(picture). This function should take 1 parameters: the picture you are going to modify. Your function should use what you learned today in class to make the changing of the hair color better.

Example

Before After

Hint

Like previously, you want to iterate over all of the pixels of your image. To do this, you need to use a for loop over the pixels of the image. You can get a listing of the pixels using the getPixels(a_picture) function.

getColor(a_pixel) will give you the color of a pixel from the image.

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(hair_color, pixel_color) will return a floating point value representing how far apart the colors are.

The setColor(pixel, color_value) function can be used to set a specific pixel to a specific color.

Use the explore tool to determine a "box" that you want to change the hair color in. It doesn't have to be perfect, but you should try to make it so that you don't have any areas with a similar color to the hair are inside of the box.

Use the if statements to only allow for pixels that are inside of the box. If the box has coordinates \(x_1, y_1, x_2, y_2\), then your condition can check to see \(if(x_1 < x < x_2 \mbox{ and } y_1 < y < y_2)\).