The objective of this mini-lab is to experiment with combining blurring with scaling.
# 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
quarter
function from a previous lab.
(Or copy it from the lab, CopyInto and Scaling.)
# 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
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!)
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.
Analysis Questions: What happens if you scale a picture up and then scale that scaled-up picture down (i.e., you usequadruple
and thenquarter
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?