copyDemo code
# Several copy functions that do simple transformations while copying. Most don't need
# the targetX, targeY incrementing given in the book examples. (Warning you *do* need
# that for scaling up.)
def copyDemo(sourceFile, canvasFile):
source = makePicture(sourceFile)
canvas = makePicture(canvasFile)
show(source)
show(canvas)
copyWithTransformation(source, canvas)
repaint(canvas)
def copyWithTransformation(src, dest):
pass # Replace this line with a call to one of the following functions
# Straight copy. No need for targetX or targetY
def copy(src, dest):
for x in range(1, getWidth(src)):
for y in range(1, getHeight(src)):
srcPixel = getPixel(src, x, y)
destPixel = getPixel(dest, x, y)
setColor(destPixel, getColor(srcPixel))
# Copy with an x and y offset.
def copyWithMove(src, dest):
xOffset = 100
yOffset = 100 # Ideally these values should be variable inputs to the function
for x in range(1, getWidth(src)):
for y in range(1, getHeight(src)):
srcPixel = getPixel(src, x, y)
destPixel = getPixel(dest, x+xOffset, y+yOffset) #Same as copy except for this line
setColor(destPixel, getColor(srcPixel))
# Copy with 90 degree counter-clockwise rotation.
def copyWithLeftTurn(src, dest):
for x in range(1, getWidth(src)):
for y in range(1, getWidth(src)):
srcPixel = getPixel(src, x, y)
destPixel = getPixel(dest, y, x) # Just flip the x,y of the corresponding pixel!
setColor(destPixel, getColor(srcPixel))
# Copy with reflection about the vertical axis.
def copyWithLeftRightSwap(src, dest):
for x in range(1, getWidth(src)):
for y in range(1, getHeight(src)):
srcPixel = getPixel(src, x, y)
destPixel = getPixel(dest, getWidth(src)-x, y) #(Max value - x) flips all x's
setColor(destPixel, getColor(srcPixel))
# Copy with 180 degree rotation is equivalent to reflecting horizontally and vertically.
def copyWhileTurningUpsideDown(src, dest):
for x in range(1, getWidth(src)):
for y in range(1, getHeight(src)):
srcPixel = getPixel(src, x, y)
destPixel = getPixel(dest, getWidth(src)-x, getHeight(src)-y) #Flip both x and y
setColor(destPixel, getColor(srcPixel))
# Copy with scaling down. I didn't show this in class. It's very similar to copying with
# an offset. Instead of the corresponding pixels being related by addition, they are
# related by division. Ideally, the scaling factor (here =2) should be input variable
def copySmaller(src, dest):
for x in range(1, getWidth(src)):
for y in range(1, getHeight(src)):
srcPixel = getPixel(src, x, y)
destPixel = getPixel(dest, (x+1)/2, (y+1)/2) #Halves the size of the source image
setColor(destPixel, getColor(srcPixel))
# Copy with scaling up. I didn't show this in class, either. It's very similar to copying with
# scaling down, BUT IT DOESN'T WORK. Try it out. What is going wrong? Can you see why the
# textbook solution, involving separate management of srcX, srcY and destX, destY is necessary
# in this case?
def copyBigger(src, dest):
for x in range(1, getWidth(src)):
for y in range(1, getHeight(src)):
srcPixel = getPixel(src, x, y)
destPixel = getPixel(dest, x * 2, y * 2) #Doubles the size of the source image, but...
setColor(destPixel, getColor(srcPixel))
# Do chromakey copying. Ideally, background to be ignored should be an input color.
# Here it is just hard-coded as white, because that works with the mediasources flower.jpg.
def copyWithChromakey(src, bg):
for x in range(1, getWidth(src)):
for y in range(1, getHeight(src)):
# This time we need to copy *selectively*
srcPixel = getPixel(src, x, y)
srcColor = getColor(srcPixel)
if distance(srcColor, white) > 10: # Substitute your background if not white
bgPixel = getPixel(bg, x, y)
setColor(bgPixel, srcColor)
# This works for the mediasources flower image, where the background is almost white.
# The blending is 0.75 foreground to 0.25 background. So background shows through faintly.
def copyWithChromakeyAndBlend(src, bg):
for x in range(1, getWidth(src)):
for y in range(1, getHeight(src)):
srcPixel = getPixel(src, x, y)
srcColor = getColor(srcPixel)
srcRed=getRed(srcPixel)
srcGreen=getGreen(srcPixel)
srcBlue=getBlue(srcPixel)
if distance(srcColor, white) > 10:
bgPixel = getPixel(bg, x, y)
newRed = (getRed(bgPixel) + 3 * srcRed) / 4
newGreen = (getGreen(bgPixel) + 3 * srcGreen) / 4
newBlue = (getBlue(bgPixel) + 3 * srcBlue) / 4
newColor = makeColor(newRed, newGreen, newBlue)
setColor(bgPixel, newColor)
Link to this Page