CS 111

Fall 2016

Introduction to Computer Science

HW10: Image processing

Due: Wednesday, 11/02 at 23:55

Prelude

In this class, we spent most of the time working with text. We also used Zelle's graphics library to draw some rudimentary shapes. This homework will introduce basics of image manipulation.

Just like when using the graphics library, we will think of an image as a two-dimensional array of picture elements called pixels, each having red, green, and blue values (each an integer between 0 and 255). (Images are usually not stored this way, since this would take up too much space. But let us not worry about that for now.)

There are a lot of things we can do with image processing. Some tasks are quite difficult (think facial recognition) but others are very easy. For example, turning a colour image into black-and-white (grayscale) is fairly simple: for each pixel, take the average of its red, green, and blue values, and use that average as the new red, green, and blue values for the pixel.

There is a nice image-manipulation library for Python known as Pillow. This library is installed on the Macs in our labs. If you want to use it on your own computer, you can follow these installation instructions. If you have trouble installing Pillow, you could also use x2go (you will need to log in to see the instructions).

For this assignment, you will implement the functions whose interfaces (parameters and return values) are specified in the skeleton file images.py. Do not change the interfaces in any way. We have discussed in class why interfaces should not be changed--so other programs relying on your functions can call them according to the interface specifications and expect the correct behaviour. When you hand in your program, please include the testing code, found at the bottom of images.py.

Every Pillow functionality you need for this assignment is already used at least once in the sample code in images.py. However, if you like, feel free to read the documentation, but you should NOT use any additional functionality.

The Skeleton File

I will use this picture of a cute teddy bear as a running example. You should test your code on a variety of pictures.

The images.py skeleton file has two sample functions already written:

To test that your Pillow installation is working properly, you can download teddy.jpg into the same directory as your copy of images.py and run python3 images.py teddy.jpg. You should see all three images appear on screen.

Basic Image Manipulations

For each of the function below, create and return a new image that is a conversion of the given image in some way: The tasks above are all fairly simple; each should take you a few lines of modification to the sample code. Implementing those correctly will get you most of the points. The following task is much harder and will be portioned much fewer points. As such, I suggest you make sure you finish the tasks above before proceeding to the next section.

Vacation Challenge

Movies, particularly action movies that use a lot of special effects, often use a technique called chroma key. With chroma key, the actors in a scene are filmed as they perform in front of a solid colour screen (perhaps green, or blue). Later on, after the scene has been filmed, the special effects people remove that colour from the scene and replace it with the actual scene: a dinosaur, the New York City skyline, outer space, etc.

In this section, you will be using the same technique to insert an image of me in front of a blackboard into a more pleasing background. (Feel free to use your own pictures.)

We can "combine" these images into a single image by replacing the "blackboard" pixels in a person picture with pixels from a scenery picture. To do this, we have to figure out which pixels correspond to the blackboard (and can be changed) and which ones correspond to the person (and should be left alone).

Hint: you should try to figure out, solely based on a pixel's red, green, and blue values, whether a pixel is likely part of the blackboard. It is okay to have some small amount of false negatives (a blackboard pixel not counted as blackboard) and false positives (a non-blackboard pixel counted as blackboard).

Submission and Grading

Upload to Moodle your modified images.py.

Optional: upload an image of yourself in front of a blackboard and an image of your dream vacation spot.

For the "basic" portion of the assignment, we will use images unknown to you to test your code.

For the "vacation" portion of the assignment, given how difficult it is to identify "blackboard" pixels in general, we will use the exact image of me linked above or your images if supplied.

This is a somewhat long assignment. Start early! Ask questions. Have fun!