< Back

Lab 31: Images

As usual, create a directory to hold today's files. All programs that you write today should be stored in this directory.

$ cd ~/cs120/labs
$ mkdir lab31 
$ cd lab31 


Practice Problem 1

Write a function check_color(a_color), which takes as a parameter a list of 3 integers. Each of the integers are guaranteed to be in the range \([0, 256)\). Your function should return \(True\) if the provided parameter is "green". Consider a list green if the value at the first index is less than \(75\), if the second value is greater than \(175\), and the third value is less than \(75\). Your function should return \(False\) in all other cases.



PPM Module

Image files can be thought of as just 2-dimensional lists of pixel color values. We can even extend that thought to 3-dimensional lists, since every pixel value can be represented by a red, green, and blue value (typically abbreviated RGB). The one complexity with this representation is how to get an image file into a list of this structure. Luckily, we are giving you a module that will do just that.

The ppm.py module holds 4 functions, but you are only concerned with two of them. the read_image(file_name) function takes a string file name of a PPM image, and returns a 3-dimensional list of pixel color values. For any given pixel color list, the red value is store in the 0th index, the green value is stored in the 1st index, and the blue value is stored in the 2nd index.

The write_image(ppm_file_name, image) takes a string file name of where you want to write a PPM image, and a 3-dimensional list of the above form. This function will actually write the image out to the file system, using the file name specified in the parameters.


Green Screen

It is pretty expensive to film movies and TV shows on location now-a-days. However, with a little bit of Movie Magic, we can pretend to be anywhere in the world. Ever want to pretend you traveled to the expanses of Moscow? Now is your chance! Ever want to go to Delaware? Yeah… Delaware.

Use Google image search to find a green screen image, and a background image that is at least the same size, if not larger. You are going to write a program that will take these two images, and composite them together to create one image. You will need to convert these pictures to PPM files, as demonstrated in class.

Details

Write a function overlay_greenscreen(greenscreen_list, background_list) in a file called greenscreen.py. This function takes two parameters: an m × n 2-dimensional list of RGB values (really just a 3-dimensional list of integers), and a j × k 2-dimensional list of RGB values (but really just a 3-dimensional list of integers), where j ≥ m and k ≥ n. This function should modify greenscreen_list by replacing pixels that are mostly green, with the corresponding pixel from background_list.

As it turns out, it's really hard to get a green screen to be exactly green. Thus, it is not sufficient to check just the value of the green portion of a given pixel. You need to make sure the green value is high enough, and that the red and blue values are low enough to make sure the color is truly "green."

Example

Here is a very simplistic example of what you are trying to do:

+ =

Hint

  • The overlay_greenscreen function will need to analyze every pixel in the greenscreen_list. Use nested for loops to iterate over all indices for the rows and columns.

  • Define three global constants called RED_THRESHOLD, GREEN_THRESHOLD, and BLUE_THRESHOLD. These should be integer values in the range [0, 255]. With these variables, a given pixel is replaced with a pixel from from the background only if the red and blue colors are below this defined threshold, and the green value is greater than the threshold.

  • You will likely have to play around with the threshold values you define for the RGB values to get a better green screen effect. The thresholds are going to be very different from what you expect. Can you figure out why this is the case?

 

Challenge

It's one thing to use a green screen image you found on the Internet. It's another thing entirely to make your own green screen images! Luckily, we have this nice (hopefully pretty clean) chalkboard that we could you to mimic a green screen.

Take a picture with your cellphone (or a friends) of yourself standing in front of the chalkboard. Email the picture to yourself, and try to composite your image with a different image you found on the web. You will likely have to make drastic changes to your threshold values, but you should be able to get something that looks decent by the end.