CSC111 Lab 3 solutions

From CSclasswiki
Jump to: navigation, search

Back to Lab 3


--Thiebaut 15:52, 18 February 2009 (UTC)

Exercises 1, 2, and 3

# lab3_sol1.py
# D. Thiebaut
# this program draws a red border 10-pixels wide
# at the top and bottome of an image

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

def drawBorderBottom( pict ):
  # draw a red line at the bottom of the image
  width = getWidth( pict )
  height = getHeight( pict )
  for y in range( height-10+1, height+1 ):
    for x in range( 1, width+1 ):
      pix = getPixel( pict, x, y )
      setColor( pix, red )
  show( pict )

def drawHorizontalBorders( pict ):
  # draw horizontal border at the bottom and top of the image
  drawBorderBottom( pict )
  drawBorderTop( pict )

def main():
  # the main program.  User picks an image. Borders are added
  # the image file is unchanged by the process, only the one displayed on screen.
  pict = makePicture( pickAFile() )
  show( pict )
  drawHorizontalBorders( pict )
  repaint( pict )

Exercise 4

# mirrorv.py
# D. Thiebaut
# Program will take a picture and mirror it vertically at a given x
# coordinate (noseX).
# The user picks the file.  
# Run the program by calling the function main()

def mirrorV( pict, noseX ):
  #receives a picture, and mirrors it along the vertical axis at 
  #x-coordinate = noseX
  width = noseX * 2
  for y in range( 1, getHeight( pict )+1 ):
    for x in range( 1, noseX ):
      leftPix = getPixel( pict, x, y )
      rightPix = getPixel( pict, width-x+1, y )
      setColor( rightPix, getColor( leftPix ) )
  repaint( pict )

def main():
  # get a picture file, load it in memory, and show it

  setMediaPath()       # choose the directory where the monkey is

  pict = makePicture( getMediaPath( "monkey.jpg" ) )
  show( pict )

  # mirror the image around its middle, then repaint it
  mirrorV( pict, 191 ) # 191 is the location of the nose of the monkey
                               # This value should be changed for other pictures

Exercise 6

Here are two functions to do the job. The first one copies the fromPic (heart) picture in to top left corner of the toPic (rose) picture.

The second one is more advanced. It requires not only the two pictures, but also two offsets, an x-offset and a y-offset, to displace the fromPic when copying it over the toPic.


def copyTo( fromPic, toPic ):
  # copies all the pixels from the fromPic picture and put
  # then AT THE SAME LOCATION in the toPic Picture 
  for pixel in getPixels( fromPic ):
    x = getX( pixel )
    y = getY( pixel )
    col = getColor( pixel )
    toPixel = getPixel( toPic, x, y )
    setColor( toPixel, col )

def copyToOffset( fromPic, toPic, offsetX, offsetY ):
  # copies all the pixels from the fromPic picture and put
  # then AT THE SAME LOCATION in the toPic Picture 
  for pixel in getPixels( fromPic ):
    x = getX( pixel )
    y = getY( pixel )
    col = getColor( pixel )
    toPixel = getPixel( toPic, x+offsetX, y+offsetY )
    setColor( toPixel, col )