Project 6: The Warhol Project
The purpose of this project is to make a collection of images in the style of Andy Warhol by manipulating images, and to then make something creative of your own design.
For this assignment you're going to create three python programs. One will generate a Warhol style collage. The second will change the blue/green background screen to a different color. The last one will combine elements of the past three projects into something creative of your choice. The first two will write their results to an image file, which you can then view with your show program. The last can display something or write out an image or sequence of images.
- Write a function that will place one image inside another
In your filter.py file, create a function placeImage which takes four arguments. The first argument will be a destination Image, the second argument will be a source Image, and the last two arguments will be an x, y location to place the second Image into the first.
Using a nested for loop over the rows and columns of the source image. If i is your row loop variable and j is your column loop variable, get the pixel values from the source image at location (j, i), then set the pixel values in the destination image at location (x+j, y+i).
+ (more detail)
The function outline in comments is given below. For each comment inside the function below, you need to write one line of python code.
# place src into dst with upper left at x, y in dst def placeImage( dst, src, x, y ): # assign to rows the height of the src image # assign to cols the width of the src image # loop over each row i in src # loop over each column j in src # assign to the tuple (r, g, b) the result of calling getPixel on src with arguments (j, i) # using dst, call the setPixel method to set location (x + j, y + i) to the color (r, g, b)
The setPixel function expects a Zelle color, so use the graphics color_rgb(r, g, b) function. The color_rgb function expects values between 0 and 255 for each color channel.
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.
- Test your placeImage function
Once you have written your placeImage function, 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:
python testPlaceImage.py miller.ppm
If you run into an error saying it can't read the data from miller.ppm, try re-downloading the miller.ppm image file.
If you look at the end of test program, you will see that it writes the output to a file called duplicate.ppm. You can use your show.py program to view the duplicate.ppm image. It should show two copies of the original image, side-by-side.
As you are working with your code, remember that the Zelle graphics package can read only PPM type images.
- Write three more photo filters
Create three more photo filters that edit the colors in a Image to achieve some effect. See if you can emulate some of the Instagram effects. Your filters need to do more than simply reordering the color channels (though you are welcome to write them in addition to your three, if you like the effect).
Some examples of good single pixel filters to try include:
- Make a sepia tone version of the image
- Make a color negative
- Apply a gamma correction (brighten or darken shadows)
- Modify the white balance
- Emphasize or saturate a certain color
- Produce a vignette effect (darker around the edges)
- Enhance or decrease the contrast
- Quantize the colors (i.e. allow only 4 levels each of red, green, and blue)
If you want to get a little fancier, you can try some filters that use information from multiple pixels. Think carefully before trying it. To keep things simpler, you can ignore pixels around the edges of the image.
- Blur an image
- Sharpen an image
- Produce an edge image
- Write a function to generate a Warhol style image
Your main warhol program (which you should put in a file named warhol.py) should read in one image, create four copies of it, use your filter functions to alter them, create a new blank image that is width*2 by height*2 and then use the placeImage function to insert the four edited images. Finally, it should write out the collage image to a new image file.
You can use the test file as an example of placing two images next to each other. In warhol.py you will need to clone the image 4 times and use a different filter on each clone.
Finish up this task by putting a call to main inside the conditional statement we've used before. Then call your python program and view the collage.
In your report, include a picture of your Warhol-style image using 4 different filters. This is required picture 1.
- Write a program to replace the background of a greenscreen image with another image.
In your filter.py file, write a function placeImageNoBkg that removes the background of a green/bluescreen image. It will be identical to placeImage, except that it will not transfer blue/green screen pixels into the destination image.
The function will need to loop over each pixel in the src image and test if it is very blue/very green. If it is not very blue/very green, then copy the pixel to the destination image.
A reasonable test for 'very green' is if the green channel is greater than 1.5 times the red channel and the green channel is also bigger than the blue channel. You can do something similar to define 'very blue'.
Create a program, greenscreen.py, that replaces the background of a greenscreen image with another image. It should take two command line arguments, one argument will be the greenscreen image filename and the other will be the new background image filename. Have the program save a new file in which the greenscreen image's background has been replaced with the new background image.
In your report, include a picture demonstrating an altered background. This is required picture 2.
- Write a program that combines elements of the last three projects
Do something creative with the Zelle graphics. You can create a static scene, an animated scene, or something that mixes images with graphics and text. For example, you could create a meme generator that combines an image, a filter, and some text provided on the command line to generate an image.
The guidelines are that the program needs to make use of at least one image and incorporate at least two of the following elements.
- Graphics shapes (e.g. Circle, Rectangle, Polygon, ...)
- An image with the blue/green screen removed
- One or more image filters
In your report, include a picture or screen capture movie of your program at work. Please include instructions on how to run it/use it.
- What is a pixel?
- What is the difference between an image filename and an Image object?
- Why are nested for loops useful for manipulating images?
- What is your favorite Andy Warhol print?
Extensions are your opportunity to customize your project, learn something else of interest to you, and improve your grade. The following are some suggested extensions, but you are free to choose your own. Be sure to describe any extensions you complete in your report. Include pictures.
- Create a larger or more complex Warhol style collage. Place the images in a pattern more complicated than that of a square (but not random!).
- Enable your Warhol program to read in multiple images from the command line and make a collage for each one, or a collage that integrates several images.
- Make a function or functions that modify the image based on one or more parameters and demonstrate that you can make several different output images by varying the parameter.
- Make more/more interesting filters.
- In your final task, integrate more elements than the required two.
Add the ability to alpha blend to your placeImage function with a parameter called alpha. Alpha blending is the formal name given to blending two images.
+ (alpha blending details)
If you have two colors, (r1, g1, b1) and (r2, g2, b2), then the alpha blend of the red color band is given by the expression below. Use a similar expression for the green and blue values. Note that the graphics color_rgb function expects integers, hence the cast.
r = int(r1 * alpha + r2 * (1.0 - alpha))
Submit your code
Turn in your code (all files ending with .py) by putting it the appropriate directory in the Courses server. On the Courses server, you should have access to a directory called CS151, and within that, a directory with your user name. Within this directory is a directory named Private. Within that is a directory for each week's project where you should submit your code. Please submit only code that you want to be graded.
When submitting your code, double check the following.
- Is your name at the top of each code file?
- Does every function have a comment or docstring specifying what it does?
Write Your Project Report
If you haven't already made a new page for this report on the wiki, then make one now (Log into the wiki, goto your Personal space by selecting "Personal Space" on the menu under the Person icon, then make the page using the "Create" button. Please use the CS 151 template. Put the label
cs151s20project6 in the label field on the bottom of the page. But give the page a meaningful title.
Your intended audience for your report is your peers not in the class. From week to week you can assume your audience has read your prior reports. Your goal should be to be able to use it to explain to friends what you accomplished in this project and to give them a sense of how you did it.
Your project report should contain the following elements. Please include a header for each section.
A brief summary of the project, in your own words. This should be no more than a few sentences. Give the reader context and identify the key purpose of the assignment. Each assignment will have both a core CS purpose--such as using loops and conditionals--and an application such as making images in the style of an artist.
Writing an effective abstract is an important skill. Consider the following questions while writing it.
- Does it describe the CS concepts of the project (e.g. writing well-organized and efficient code)?
- Does it describe the specific project application (e.g. making images)?
- Does it describe your the solution or how it was developed (e.g. what code did you write)?
- Does it describe the results or outputs (e.g. did your code work as expected)?
- Is it concise?
- Are all of the terms well-defined?
- Does it read logically and in the proper order?
For each task, If you completed the task, include an image of the completed task and write a few sentences describing the image/what you did. This should be a description of the form and functionality of your final code. Note any unique computational solutions you developed or any insights you gained from your code's output.
If you didn't complete the task insert an image showing how far you got. Write a few sentences describing how much you completed, where you got stuck and what what solutions you tried.
You may want to incorporate code snippets in your description to point out relevant features. Code snippets should be small segments of code--usually less than a whole function--that demonstrate a particular concept. If you find yourself including more than 5-10 lines of code, it's probably not a snippet.
A description of any extensions you undertook, including text output or images demonstrating those extensions. If you added any modules, functions, or other design components, note their structure and the algorithms you used.
- Follow-up Questions
The answers to any follow-up questions (there will be 3-4 for each project).
A brief description (1-3 sentences) of what you learned. Think about the answer to this question in terms of the stated purpose of the project. What are some specific things you had to learn or discover in order to complete the project?
A list of people you worked with, including TAs and professors. Include in that list anyone whose code you may have seen, such as those of friends who have taken the course in a previous semester.
A list of any resources that you used. This includes textbooks, lecture notes, python documentation, library documentation or forums. If you used a specific website, please link to it.
- Put the label cs151s20project6 on your wiki page.