BYU logo Computer Science

Project 2 — Images

Complete the following problems.

Setup your environment

To begin, use PyCharm to create a project called project2. You should do all your work there.

Next, download project2.zip. This is a zip file, so you will need to extract its contents. You should put the files inside of your project2 folder. When you are done, it should look like this:

project 2 files

These files contain:

  • Python scripts where you will write functions to solve problems. For example, in the “Yellow Bars” problem below, you will hand in a file named yellow_bars.py.
  • Images that you can use for each problem. Use these to test your solutions before turning in the project.

Yellow Bars

Create an image that has yellow bars on the top and side of a photo:

yellow bars beach

For this problem, write your code in yellow_bars.py. You should write the code for the function included there:

def yellow_bars(original, thickness)

This function takes two arguments:

  • original is an image
  • thickness is the thickness of the bars

The function should return a new image, which contains the original image, but with a yellow bar on the top and right sides, using the specified thickness.

Be sure to also write documentation for this function.

Our example uses beach2.jpg, but your code should work for any image.

You can use the RGB Color Slider tool to figure out how to make yellow.

Reflect

Create a reflected photo:

reflected beach

For this problem, write your code in reflect.py. You should write the code for the function included there:

def reflect(original):

This function takes one argument:

  • original is an image

The function should return a new image, which contains the original image in the top half and a reflected image in the bottom half.

You are required to decompose this problem into at least two more functions. One of them can be the function you wrote in a previous lab to copy an original image into an arbitrary location in a new image. Another could do the work of reflecting an image. The reflect() function should call the other functions you write.

Be sure to also write documentation for your functions.

Our example uses beach1.jpg, but your code should work for any image.

Vacation Photos

You took a vacation to visit four great beaches around the world. Create an image that combines these into one photo:

vacation

Note that all four images are the same size.

For this problem, write your code in vacation.py. You should write the code for the function included there:

def vacation_photos(photo1, photo2, photo3, photo4, thickness):

This function takes five arguments:

  • photo1, photo2, photo3, and photo4 are images
  • thickness is the thickness of the border around the photos and in between the photos

The function should return a new image, which contains the four images arranged as shown above.

You are required to decompose this problem into at least two more functions. One of them can be the function you wrote in a previous lab to copy an original image into an arbitrary location in a new image. The vacation_photos() function should call the other functions you write.

Be sure to also write documentation for your functions.

Our example uses beach1.jpg, beach2.jpg, beach3.jpg, and beach4.jpg, arranged left to right and top to bottom, but your code should work for any set of images that are all the same size.

Girl on the Moon

Can you put a girl on the moon? Use the moon-landing.jpg and jumping-girl.jpg images:

moon landing

jumping girl

For this problem, write your code in girl_on_the_moon.py. You should write the code for the function included there:

def girl_on_the_moon(moon, girl):

This function takes two arguments:

  • moon is the image of the moon
  • girl is the image of the girl

The function should return a new image, which shows the girl on the moon.

This problem is very similar to what we worked on in the green screen lecture. The difference is that instead of copying the pixels from the moon onto the image of the girl (replacing the green pixels with pixels of the moon), we want to copy the girl onto the moon image (replacing the pixels of the moon with the non-green pixels of the girl).

To do this, you want to use your detect_green() function from the green screen lecture and your copy_image() function from the general functions lecture. Remember that copy_image has some parameters:

def copy_image(image, image_to_copy, x_start, y_start):

This means you can control exactly where the girl is copied. You should give it the coordinates (600, 850), so the girl will appear at the bottom, right portion of the image. Try copying the entire image (including the green pixels) to start. Then modify copy_image() so that it copies only the non-green pixels, using not and the detect_green() function. Due your best to tune the parameters for detect_green() so that you don’t have stray green pixels appearing on the moon, just the girl.

Finally, note that we have sized these images so that the moon image is quite a bit larger (1280x1280) than the girl picture (640x427). This will ensure the girl fits nicely on the moon!

Be sure to also write documentation for your functions.

Turn in your code and a copy of your image of the girl on the moon.

Points

TaskDescriptionPoints
Yellow BarsYour solution works3
ReflectYour solution works5
Vacation PhotosYour solution works7
Girl on the MoonYour solution works8
DocstringsYou have docstrings that clearly define what each function does and explain the pre-conditions and post-conditions for each function2
DecompositionYou have used decomposition to write small functions that accomplish each piece of a problem5

The auto grader on GradeScope will grade whether your solution works. The TAs will grade your docstrings and decomposition as follows:

  • Docstrings
    • 2 points: You have docstrings that clearly define what each function does and explain the pre-conditions and post-conditions for each function
    • 1 points: Your docstrings need some improvement
    • 0 points: No docstrings or only a few docstrings
  • Decomposition
    • 5 points: You have decomposed each problem into a set of small functions with well-defined tasks
    • 3 points: You have several places where decomposition needs improvement
    • 0 points: You have mostly written one long function to solve each problem

The TAs may award points between 0 and 3 and between 3 and 5 if you are in betweeen these scores.

Image credits