CS 107: Pictures and Sounds: Programming with Multimedia

Kalamazoo College

Spring 2008

Mini-Lab: Blurring and Scaling

 


Introduction

The objective of this mini-lab is to experiment with combining blurring with scaling.



Part 1: Blurring

  1. Copy and test the following version of a simple blur function (Recipe 39 in the text, slightly modified).
    # A simple blur
    def blur(source):
      newCanvas = makeEmptyPicture(getWidth(source), getHeight(source))
      for x in range(2, getWidth(source)):
        for y in range(2, getHeight(source)):
          top = getPixel(source,x,y-1)
          left = getPixel(source,x-1,y)
          bottom = getPixel(source, x, y+1)
          right = getPixel(source, x+1, y)
          center = getPixel(source, x, y)
          newRed = (getRed(top)+getRed(left)+getRed(bottom)+getRed(right)+getRed(center))/5
          newGreen = (getGreen(top)+getGreen(left)+getGreen(bottom)+getGreen(right)+getGreen(center))/5 
          newBlue = (getBlue(top)+getBlue(left)+getBlue(bottom)+getBlue(right)+getBlue(center))/5
          setColor(getPixel(newCanvas,x,y), makeColor(newRed, newGreen, newBlue))
      return newCanvas 
    

Part 2: Blurring and Scaling

  1. Copy your quarter function from a previous lab. (Or copy it from the lab, CopyInto and Scaling.)

  2. Copy and test the following version of a function to make an image 4 times as large. (To test this function, use a relatively small picture.)
    # Returns a new picture 4 times the size of the given
    # original picture (twice as wide and twice as high).
    def quadruple(orig):
      #Get the original width and height and divide them in half
      newWidth = getWidth(orig)*2
      newHeight = getHeight(orig)*2
    
      #Make an empty picture to store the scaled image
      newCanvas = makeEmptyPicture(newWidth,newHeight)
    
      #Copy every other pixel onto the new canvas
      for targetX in range(1, newWidth+1):
        for targetY in range(1, newHeight+1):
          color = getColor(getPixel(orig, max(1,targetX/2), max(1,targetY/2)))
          setColor(getPixel(newCanvas, targetX, targetY), color)
      return newCanvas
    

  3. Write a function called scaleUp which takes a picture as a parameter, creates a quadrupled version of the picture, and then blurs this large picture. It should return the blurry large picture. (Hint: If your function has more than a couple of lines, you are doing too much!)

  4. Test this function with several pictures.

  5. Write a function called scaleDown which takes a picture as a parameter, creates a blurry version of this picture, and then creates the quartered version of the the blurry picture. It should return the small picture.

  6. Test this function with several pictures.
    Analysis Questions: What happens if you scale a picture up and then scale that scaled-up picture down, and vice-versa? Do you get the original picture back? Why or why not? Does one look more like the original than the other? Why might this be?

Print your results

  1. Show your results to your instructor when you are finished.
  2. Print the functions you created in this lab and hand them in.