Project 4: The Warhol Project

The purpose of this project is to make a collection of images in the style of Andy Warhol. You'll do this by manipulating the pixel colors of an image.

alt text

Here is the reference guide to the graphics package.


Make sure you have copies of the and files.

For this assignment you're going to create two python programs. One will generate a Warhol style collage. The other will change the blue screen to a different color. Both will write their results to an image file, which you can then view with your show program.

As you are working with your code, remember that the Zelle graphics package can read only PPM type images.

1. Arrange images side-by-side with the placeImage function

  1. Read through the following placeImage function, then add it to your file.
def placeImage(dst, src, x, y):
    '''Place src into dst with upper left at x, y in dst
        dst: destination image
        src: source image
        (x,y): location to place the second Image into the first
    # loop over each row i in src
    for i in range(src.getHeight()):
        # loop over each column j in src
        for j in range(src.getWidth()):
            # assign to the tuple (r, g, b) the result of calling
            # getPixel on src with arguments (j, i)
            (r, g, b) = src.getPixel(j, i)
            # Using dst, call the setPixel method to set location
            # (x + j, y + i) to the color (r, g, b)
            # The color_rgb function expects ints between 0 and 255
            # for each color channel.
            dst.setPixel(x + j, y + i, graphics.color_rgb(r, g, b))


  1. Once you have added the placeImage function, you can use this file to test it. Run the test program on the command line and give it an image filename as its argument. For example:
python3 miller.ppm

If you look at the end of test program, you will see that it writes the output to a file called duplicate.ppm.

  1. Use your program to view the duplicate.ppm image. It should show two copies of the original image, side-by-side.

    Answer the following questions about placeImage in your report:
    a. Why do we access and save the RGB pixel value of src?
    b. Why are we looping through rows and columns in src rather than dst?
    c What is the meaning of the i and j offsets in dst? How about x and y?

  2. Once you are confident that your placeImage function works, please remove the comments that we supplied. They don't help the reader understand the overall purpose of the code because they are a line-by-line annotation of what each line does. In most cases, that is obvious from the code itself. Helpful comments include the docstring (which you need to write), which explains the meaning of each parameter.

2. Instagram-style effects

Add code to Create three more functions like swapRedBlue that edit the colors in a Image to achieve some effect. See if you can emulate some of the Instagram effects. Note: when we say "like swapRedBlue", we do not mean "variants of swapRedBlue".

Note: A filter that simply reorders the color channels will not count towards your three (though you are welcome to write them in addition to your three, if you like the effect).

Hint: You will likely make use of set and get methods for Image objects. Review the code from lab and placeImage.

3. 2x2 Warhol collage

Make your main warhol program that you should put in a file named Code that implements the following algorithm should be placed within a function called main.

  1. Have the main function take a parameter called argv. This will represent the user-specified input arguments from the command line with the filename string of the image that we want to load in for our Warhol 2x2 collage.

    Note: You must be sure that you refer sys.argv only in your top-level code (inside if __name__ == "__main__":). If you have another function that accesses the command line arguments via sys.argv, then you are dramatically restricting the breadth of programs that can import and use that function.

  2. Check to make sure that the user passed in at least one argument via the command line. If not, print a message instructing the user to do so and exit the program.
  3. Load in the image and assign the reference to the Image object to a variable (e.g. img).
  4. Clone your Image object four times, then apply one of Instagram-style filters on each.
  5. Create an blank Image object that has twice the width and height of the image that you loaded in. This will be your Warhol collage.
  6. Use your placeImage function to arrange your four filtered images on the larger blank Image that you just created on a 2x2 non-overlapping grid. For example, the first filtered image should start at (x, y) = (0, 0). The first filtered image should be placed to its right should be placed at (x, y) = (width, 0), etc.
  7. Save your large collage image to a file.

Finish up this task by putting a call to main inside the conditional statement:

if __name__ == "__main__":
    # Call to main here, with command line arguments passed in


In your writeup, include a picture of your Warhol-style image. This is required picture 1.

4. Blue/green-screen image

Your last task is to create a python program that reads in your blue or green-screen image (your choice) and turns the background pixels to a different color. Call it or, depending on the background color of you image (the images will be placed in Courses/CS151/CourseMaterials). The rest of the pixels should remain untouched.

You will need to use a nested loop to test if every pixel in the image is very blue/very green. If it is very blue/very green, change its color. Otherwise, leave it alone. A reasonable test for 'very blue' is if the blue channel is 1.5 times the red channel and also bigger than the green channel. You can do something similar to define 'very green'.

In your writeup, include a picture demonstrating your altered background. This is required picture 2.


Congratulations on completing the core tasks! Between the code (and comments) that you’ve already written and the report (below), you’ve already earned up to 26/30 points for this project. The remaining 4/30 are a chance for you to earn credit for exploring parts of this project and digging deeper to express your creativity! If you’re exhausted or running out of time, this is a perfectly respectable place to stop. If you’re up for it, you have the option to design your own extension or pick one of the following examples. To earn all 4 extension points, concentrate on one design challenge, and really dig into it both computationally and creatively. What will you create?

Here are some example extensions:

Remember: We award extension points based on the depth of your computational exploration. A good extension should interest you and show us that you can apply this week's material in creative ways.

Written Report


  1. Put the python files you wrote on the Courses server in your private directory in a folder named Project4.
  2. Attach a new Google Doc page for your assignment on Google Classroom.
  3. Put the label cs151s19project4 on the report page.


Remember, the written report counts for 5/30 points that you earn for the project.

Report checklist

In general, your report should follow the outline below.