CS 111: Introduction to Computer Science

Image processing

Hand in via Moodle as images.py.

You may work with a partner of your choosing or alone. If you want to be assigned a partner, send me a private Slack message.

Goals

The project

If you have ever used image-manipulating software (e.g. Adobe Photoshop, Affinity Photo, etc.), you know that you can do lots of crazy things to digitized images. Some of the games you can play involve very complex mathematics, but others are pretty easy. For example, turning a color image into a black-and-white image is simple: for each pixel, take the average of the pixel's 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 the Python Imaging Library, or PIL. You will use PIL to implement a few interesting image transformations.

More specifically, for this assignment, you will implement the functions whose interfaces are specified in images.py.

Where to start

The images.py file has two sample functions already written: get_green_image and get_small_image. If you executed this code:

image = Image.open('maisie.jpg') green_image = get_green_image(image) green_image.show()
on the picture of my dog Maisie on the left, you would get the picture on the right:

  

With a simple modification of the code in get_green_image, you can write get_black_and_white_image. The idea here is to replace the red, green, and blue values of a given pixel with the average of the original red, green and blue values at that pixel. For example, if a pixel has the color values (100, 80, 30), you would replace that pixel with (70, 70, 70).

Similarly, the get_rotated_image function (whose job is to rotate the image clockwise by 90 degrees) is not too hard, but it does require you to create a new image with different dimensions than the original. For example, if the original image has image.size equal to (1000, 750), then the new image would need to have image.size equal to (750, 1000). See the code in get_small_image for an example of how to create a new image with specified dimensions.

Basic and Advanced assignments

To be eligible for a score of 15 out of 20 points, you need to implement all the functions labeled REQUIRED. To be eligible for 18 of 20, implement all the REQUIRED functions and at least two of the OPTIONAL functions. And to be eligible for 20 of 20, implement all of the functions.

REQUIRED functions

These functions applied to the picture of Maisie would result in the following images. (I called get_bordered_image with color (120, 40, 40) and border thickness 60.)

  

  

OPTIONAL functions

A few extra thoughts

Start early, ask questions, and have fun!