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.
    # A simple blur
    def blur(source):
      newCanvas = makeEmptyPicture(getWidth(source), getHeight(source))
      for x in range(1, getWidth(source) - 1):
        for y in range(1, getHeight(source) - 1):
          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 multiply them by 2.
      newWidth = getWidth(orig) * 2
      newHeight = getHeight(orig) * 2
    
      #Make an empty picture to store the scaled image
      newCanvas = makeEmptyPicture(newWidth,newHeight)
    
      #Every pixel in the original picture is copied to four pixels
      #in the new picture...
      for targetY in range(newHeight):
        for targetX in range(newWidth):
          color = getColor(getPixel(orig, targetX / 2, 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 (i.e., you use quadruple and then quarter without any blurring), 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?

Submit your results

  1. Submit the file you created in this mini-lab via Kit.