CSC111 Homework 5

From CSclasswiki
Jump to: navigation, search

--Thiebaut 13:09, 25 February 2009 (UTC)

The Problem at Hand

Write a program that copies the picture of the mask onto the the picture of the Jester, then blurs the picture.

CSC 111 mask.jpg + CSC111 mardigras.jpg = CSC111 blurredMardiGras.jpg


Note that the mask is copied only over white areas of the jester picture, and not over the jester itself!

Recommendation

Use the Top-Down approach!

Requirements

Submission

  • Submission: You should submit one folder containing 1 program with the .py extension ,and one image, with a .jpg extension. The name of the folder should be your first initial, last name, and HW5 appended to the end (for example I would call mine DThiebautHW5).
  • The jpg picture submitted will be a copy of the blurred jester with masks copied around it (or a picture of the jester with hearts copied around it, if you do the extra-credit question).

Main function

  • Main(): Your main program must return the new picture. We should be able to see the image generated by your program by:
    • loading your program in the program area, and
    • typing
>>> jester = makePicture( pickAFile() )
>>> mask = makePicture( pickAFile() )
>>> show( main( jester, mask ) )

Blurring 8, not 4

  • Your blur function should use all 8 neighboring pixels, not just the four that are top, right, bottom and left.

Making Multiple Copies of a Picture

The trick in this assignment is to copy multiple times the small picture onto the larger one. Forget for right now that we do not want to overlap the jester. Just concentrate on copying the mask all over the jester picture.

CSC111 picInPic.png

Imagine that you must copy a small picture that is 3 by 4 (the size of a colored rectangle above) multiple times over a larger picture that 7 by 9 (the large rectangle above).

We have seen and written a function before, copyToOffset(), that can place a small picture into a larger one at any offset we want.

Your assignment is to make your program decide, on its own, how to plaster the small picture all of the large one. In the case above, the program should figure out that it could use offsetX = 0, 3, and offsetY = 0, 4, and only these values so that it can copy the small image four times, so that its top left corner overlaps with the pixels at (1, 1), (1, 4), (5, 1), and (4,5).

The range function can help us out. If you provide a 3rd parameter to range, it will use it as a step value. This is best illustrated by trying these examples on your own in the console:

>>> range(1, 10 )
>>> range(1, 10, 1 )
>>> range(1, 10, 2 )
>>> range(1, 10, 3 )
>>> range(0, 10, 4 )
>>> range( 10, 0, -1 )
>>> range( 10, 0, -3 )
>>> for x in range( 1, 10, 2 ):
...     for y in range( 1, 10, 5 ):
...       print x, y


In your top-down approach, write a function that plasters the mask all over the jester, multiple times, without worrying about totally overlapping.

Copying but not overlapping

For this assignment, you will be provided with an algorithm for making sure the small image does not overlap something other than the background of the large image. Up to you to implement it in Python:

Algorithm

Assumption #1: the offsetX and offsetY values that define where the top-left corner of the small image is supposed to go are known.
Assumption #2: To find out what the background picture of the large image is, use the color of its top-left pixel.
  1. identify where the 4 corner pixels of the small image will be mapped in the large image
  2. if the color of all 4 corner pixels in the large image are of the same color as the background picture, then we can copy the small image, otherwise we will overwrite some part of the jester.

Extra Credits

Extra credits (the max grade will be 12/10, or A+), if your program sports the following features:

  • It uses a boolean function (which returns either True or False) depending on whether the small picture can be copied onto the large one or not. One could use it in the following way...
   ...
   if canCopy( smallPic, largePic, x, y )==True:
        # copy 
   else:
        # don't copy
  • Your program works also with small pictures that have a background different from white. For example, if we give your main function the picture of the jester and the picture of the heart from Lab 3 instead of the mask, it copies only the red heart over the jester picture, and none of the black pixels that form the background of the heart.