< Back

Lab 19: Images


Practice Problem 1

Write a function called convert_grade(numeric_grade), which takes an integer in the range \((0, 5)\). Your function should print the letter grade the user recieved, given the numeric grade:

Numeric Grade Letter Grade
0F
1D
2C
3B
4A

Example

>>> convert_grade(0)
F
>>> convert_grade(1)
D
>>> convert_grade(4)
A

Hint

You are directly given an integer as input. You simply need to convert this integer into a string. There are many ways to do this, but the easiest to understand is likely using if statements.

For each entry in the table above, you need one if statement. if the numeric value equals 4, then output A, etc.

Don't forget, to compare equality you want to use the == operator.


Practice Problem 2

Write a function called print_factors(x), which prints all of the positive integer factors of \(x\). An integer \(y\) is an integer factor of \(x\) if the remainder of \(\frac{x}{y}\) is 0.

Example

>>> print_factors(1)
1
>>> print_factors(2)
1
2
>>> print_factors(4)
1
2
4

Hint

The remainder operator in Python is \(\%\). \( x \% y == 0\) tells you that y evenly divides x.

Write a loop that goes through all of the integers greater than 1, but less than (or equal to) \(x\). See if the above condition is true. If it is true, print the loop variable.


Blurred Images

In a file called blurred_images.py write a function called simple_blur(a_picture). This function should display a blurred version of the image parameter.

To blur an image, you simply average a pixels color value with the 4 pixels surrounding it.

Example

Before After

Note: This image has actually been blurred 4 times, to make it obvious what is happening. Your image will only be slightly fuzzy.

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 also need to get the 4 pixels surrounding your current location. You can use the getPixelAt(a_picture, x, y) function to get a pixel at a specific location from the picture. You simply need to add and subtract 1 from x and y each.

For each of these 4 surrounding pixels, you want to getRed, getGreen, and getBlue. You will compute the average of each of those values, and use setRed, setGreen, and setBlue to set the color of the pixel.