Project 4: The Warhol Project
For this assignment you are going to create two Python programs, warhol.py and bluescreen.py. The first will generate a collage after the style of pop artist Andy Warhol. The other will change the blue screen that appears in your photograph to a different color. Both will save their results to an image file that you can view with the show.py program you created in lab.
Make sure you have a copy of the pilutil.py file and the show.py and effects.py files you created in lab.
Tasks
- Within your effects.py file, create a function named
putImage() that expects four parameters. The first parameter will be
a destination Image, the second parameter will be a source Image, and the last
two parameters will specify a (x, y) location to place the source Image data in
the destination Image. The function outline is given below in comments.
def putImage( dest, source, x, y ): ''' Copies data from a source image into a destination image. Parameters: dest - the destination PIL Image, must be large enough to hold the source image source - the source PIL Image x, y - the location in the destination image of the upper left corner of the source image ''' # find the width and height of the source image # for each row in the source image # for each column in the source image # from source, get the (r, g, b) value at pixel (column, row) # in dest, set location (x + column, y + row) to the value (r, g, b) - When you have written your putImage function, you can use the
following function to test it. Copy this function into your file and try using
it, passing in the name of a jpg file as an argument.
def testDuplicate( filename ): ''' Loads an image from a file, creates a new image of twice the width, and copies the original image into the new image twice. The new image is saved in a file named 'duplicate.jpg' Parameters: filename - the name of the original image file. ''' # loads an image and creates a blank image twice the size srcImage = Image.open(filename) (width, height) = srcImage.size dstImage = Image.new("RGB", (width * 2, height)) # copy the original image into the new image twice putImage( dstImage, srcImage, 0, 0 ) putImage( dstImage, srcImage, width, 0 ) # save the image to a file dstImage.save( 'duplicate.jpg' )This is required image 1.
- Within effects.py, create three more functions like swapRedBlue() that edit the colors in a PIL Image to achieve some effect.
- Create a file named warhol.py, and import the Image,
effects, and sys modules. The main function for your
Warhol program should read in an image, create four copies of it (e.g.
copy1 = original.copy()), use a different manipulation function to
change each copy, create a new blank image of size (2*width x 2*height), and
insert the four modified images into this blank image. Finally, it should save
this collage image.
def main(args): # if the length of args is less than 2 # print a usage statement # exit # assign to a variable original the result of opening the Image named by argv[1] # assign to a variable copy1 the result of copying original # call your first manipulator function on the copy # assign to a variable copy2 the result of copying original # call your second manipulator function on the copy # assign to a variable copy3 the result of copying original # call your third manipulator function on the copy # assign to a variable copy4 the result of copying original # call your fourth manipulator function on the copy # find the width and height of the original image # assign to a variable named collage a new Image of size (2*width x 2*height) # put copy1 into the collage at (x, y) = (0, 0) # put copy2 into the collage at (0, height) # put copy3 into the collage at (width, 0) # put copy4 into the collage at (width, height) # save the collage to a fileFinish up this task by putting a call to the main function inside the conditional statement we have used before. Then run your Python program and view the collage it generates.
This is required image 2.
- Your last task is to create a Python program that reads in your blue screen
image and turns the blue screen pixels to a different color. The rest of the
pixels should remain untouched.
Create a function in effects.py that loops over each pixel in an input Image and tests if it is very blue. If the pixel is very blue, change its color. Otherwise, leave it alone.
A reasonable test for a 'very blue' pixel is if the blue value is at least twice the red value and also bigger than the green value.
Create a file named bluescreen.py that uses this effect function to modify an image that contains a blue screen.
This is required image 3.
Extensions
- Create more effects and a bigger Warhol style collage.
- Do something more interesting than replacing the blue screen with a single color. For example, use pixel manipulations to generate a checkerboard, color gradient, or some other interesting pattern.
- Place yourself in a scene. Start with a background scene that is an image of the same size as your blue screen image.
- 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, and saves the collages to another file specified on the command line.
- 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 a function that modifies the spatial arrangement of pixels, not just the color values. For example, create a mirror image.
- Consult the PIL documentation to learn how to use pixel access objects, and use these to make your image manipulation functions faster.
- Consult the PIL documentation and use some of the image manipulation modules it provides. For example, apply an image filter like edge finding, or draw some interesting graphics on your images.
Write Up
Remember, there are two components you need to hand in: your code and a write up.
For this assignment, you will work on the Results section of the write up. The write up should include the images created as output from your programs along with brief descriptions. Be sure to include a description and image that demonstrates any extensions you undertook. You do not need to complete the other sections of the write up.
Add the following label to your Wiki page. Then follow the link to see the list of all pages that have been labeled. Ensure your page appears in this list (it may take a few minutes to appear).
cs151s12project4