The objectives of this lab are to become more familiar with techniques for blending images and swapping the background of an image.
swapbg), and 43 (chromakey).
Run them with the same images that they used in the text - these can
be found in the MediaSources folder. Do you get the same results?
swapbg and chromakey. Try placing
yourself in the jungle. You may need to alter the definition of blue
in the chromakey function, since we used a green
background. You can use MediaTools in JES to help
you figure out what our green is. You also need to have both pictures
the same size. If you need to change the size of a picture you may use
the cropPicture function described on your JES shortcut sheet
or write your own function to generate a picture of the correct size.
Note: Theswapbgandchromakeyfunctions modify the picture passed in as the first parameter. To experiment repeatedly with the same picture, you will have to use a new copy of the source picture each time. You could do this by creating a variable to contain a duplicate of the source picture, and then making your alterations to the duplicate picture. So, you might add a line such aspict = duplicatePicture(source)as the first line in these functions, and then instead of usingsourcein the function, use the variablepict. You should then remember to return your new picture at the end of the function.
writePictureTo if
you have forgotten how to use it.)
def blendPictures(pict1, pict2, overlapAmt):
width1 = getWidth(pict1)
height1 = getHeight(pict1)
width2 = getWidth(pict2)
height2 = getHeight(pict2)
# Set up width and height for new canvas
newWidth = width1 + width2 - overlapAmt
newHeight = min(height1, height2)
# Create the canvas to hold the blended pictures
newCanvas = makeEmptyPicture(newWidth, newHeight)
# Copy the first picture up to the overlap section
for x in range1(1, width1 - overlapAmt):
for y in range1(1, newHeight):
color = getColor(getPixel(pict1, x, y))
setColor(getPixel(newCanvas, x, y), color)
# Copy the blended section
# 50% pict1 and 50% pict2
pict2_x = 1
for pict1_x in range1(width1 - overlapAmt + 1, width1):
for y in range1(1, newHeight):
pixel1 = getPixel(pict1, pict1_x, y)
pixel2 = getPixel(pict2, pict2_x, y)
newRed = 0.50 * getRed(pixel1) + 0.50 * getRed(pixel2)
newGreen = 0.50 * getGreen(pixel1) + 0.50 * getGreen(pixel2)
newBlue = 0.50 * getBlue(pixel1) + 0.50 * getBlue(pixel2)
color = makeColor(newRed, newGreen, newBlue)
setColor(getPixel(newCanvas, pict1_x, y), color)
pict2_x = pict2_x + 1
# Copy the remaining section of pict2
targetX = width1 + 1
for x in range1(overlapAmt+1, width2):
for y in range1(1, newHeight):
color = getColor(getPixel(pict2, x, y))
setColor(getPixel(newCanvas, targetX, y), color)
targetX = targetX + 1
# Return the new canvas
return newCanvas
Run this function with several different images and overlap amounts. Are the results what you expected? Save one set of blended images to post on your web page.
kzoo.edu. (You may want to review
the instructions in Lab 1 if you've forgotten how
to do this.)