CSC111 Lab 3

From CSclasswiki
Jump to: navigation, search

--Thiebaut 03:26, 10 February 2009 (UTC)

This lab deals with nested loops, mirroring and copying pictures.

Exercise 1: Drawing horizontal borders

The functions below will draw a horizontal red line at the top and at the bottom of a picture.

def drawBorderTop( pict ):
  # draw a red line at the top  of the image
  width = getWidth( pict )
  for x in range( 1, width+1 ):
    pix = getPixel( pict, x, 1 )
    setColor( pix, red )
  repaint( pict )

def drawBorderBottom( pict ):
  # draw a red line at the bottom  of the image
  width = getWidth( pict )
  height = getHeight( pict )
  for x in range( 1, width+1 ):
    pix = getPixel( pict, x, height )
    setColor( pix, red )
  repaint( pict )
  • Create a third function called drawHorizontalBorders(...) that calls the previous 2 functions to draw both borders together.
  • Pick an image of your choice and test the two functions. Verify that you get a line at the top and at the bottom of the image.
  • Modify the first function so that it displays a border that is 10 pixels wide. (Hints: this involves nested for-loops, where the first loop will make y vary from 1 to 10, and the inner loop will control x...)
  • Test your modified function.
  • Modify the second function that it, too, will draw a line 10-pixels wide at the bottom of the image

Exercise 2: Drawing vertical borders

  • You do the same thing, but this time you come up with two functions that draw the vertical borders, and create a new function that calls these two to draw the two borders at once. Add these functions to the previous program so all your functions are in one program.
  • Test, test, test.
  • Make sure you show your result to your instructor and get comments on your code.

Exercise 3: Drawing a border all around

  • Create a new function (a seventh one in your program) that calls the right selection of functions previously written to draw a border all around your picture.
  • Test your function on a different image to make sure it works correctly.

Exercise 4: Mirroring through the center of the image


When we mirror pixels around a vertical line in the middle of a picture, we work with pairs of pixels, one of the left of the center line, one on the right. The loop that is the workhorse for mirroring looks like this:

  width = getWidth( pict )
  height = getHeight( pict )
  for y in range( 1, height+1 ):
      for x in range( 1, ( width/2 )+1 )
           pixelLeft = getPixel(  pict, x, y )
           mirrorX = width+1 - x
           pixelRight = getPixel( pict, mirroX, y )
           col = getColor( pixelLeft )
           setColor( pixelRight, col )

To better understand how this loop works, run the short program below. It uses a similar nested loops, but instead of working on a picture it prints all the x and y values of interest. Verify that the values it prints out make sense to you!

  def testMirrorLoop():
     width = 10  # assume a picture 10 pixels wide
     height = 3  # and 3 pixels high
     for y in range( 1, height+1 ):
         for x in range( 1, ( width/2 )+1 ):
              mirrorX = width+1 - x
              print "x=",x,"y=",y,"  mirrorX=",mirrorX,"y=",y

Mirroring Images

  • Pick one of the following pictures so that you can mirror it, the way we did in class.

CSC111 symmface2.jpg CSC111 monkey2.jpg CSC111 johnnydepp2.jpg

(click on the image you want to use to get it full scale)

  • A program similar to the one we saw in class is available here . Make sure you understand it before using it!
  • These images have been set so that the tip of the nose of the "person" is exactly at the middle of the width of the picture. Look at your notes from this morning and write a function that will "mirror" the left half and copy it over to the right side.
  • See how symmetrical the face is.
  • Change your function so that it copies the right side onto the left side. See any difference between the two?

Exercise 5: Misplaced nose...

CSC111 symmface.jpgCSC111 monkey.jpg CSC111 johnnydepp3.jpg

  • This time the nose is not exactly in the middle of the width of the image any longer. It is closer to the left edge, rather than to the right edge.
  • You will have to figure out the x-coordinates of the nose. One way to do this is to makePicture() the image you will have picked, and open the picture media-tool to figure out the X-location of a pixel in the middle of the nose.
  • Once you have the x coordinate of the middle of the nose, modify the for-loop that controls the x-coordinate so that it uses this bound and not the half-width.

Exercise 6: Copy one image into another

CSC111 heart.jpg

(More information will be given during the lab)

CSC111 bigRose.jpg
  • For this part, you have to imbbed the picture of a heart inside a picture of a rose
  • The algorithm for the function that will do the merging could look like this
    • open the two pictures
    • for all x,y coordinates corresponding to the pixels in the heart picture,
      • get the color of the pixel at (x,y) in the heart image
      • put the same color in the pixel at the same (x,y) coordinates in the rose image.
This should put the heart at the top-left corner of the rose image.
  • Go ahead and copy the heart to the rose image. Make sure you demonstrate your final image to your instructor!

Exercise 7: Mirror and framed (Only if you have time)

  • Combine some of the functions you generated previously into one program and create a new function that will mirror an image and put a red border all around it.

Happy Valentine's Day!