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(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
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 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
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, 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?