CSE 1710.03A Programming for Digital Media

Lab 05

Due date: Oct 19, 2009 at 20:00

Blending two images with a gradient

In section 5.1 in the textbook there is an example program of how to blend the overlapping area of two horizontally arranged images with a constant factor. In that example, the boundaries of the original two images are clearly visible in the blended version. This problem stems from the fact that both images are blended with equal weights, i.e. both colors are mixed with 50% contribution each. A version of this code using a constant area of 100 pixels overlap is given here:
def blendimages(file1,file2):
  img1 = makePicture(file1)
  img2 = makePicture(file2)
  compw = getWidth(img1) + getWidth(img2) - 100    # composite width
  comph = max(getHeight(img1),getHeight(img2))     # composite height
  composite = makeEmptyPicture(compw,comph)        # composite picture
  for x in range(0,getWidth(img1) - 100):	# copy most of left image 
    for y in range(0, getHeight(img1)):
      color = getColor(getPixel(img1,x,y))
      setColor(getPixel(composite,x,y),color)
  for x in range(100,getWidth(img2)): 			# copy most of right image
    for y in range(0, getHeight(img2)):
      color = getColor(getPixel(img2,x,y))
      setColor(getPixel(composite,x + getWidth(img1) - 100,y),color)
  for x in range(0, 100):						# handle overlapping region
    for y in range(0, min(getHeight(img1),getHeight(img2))):
             # min() ensures that the blend extends only to the smaller image
      pixel1 = getPixel(img1,x + getWidth(img1) - 100,y)
      pixel2 = getPixel(img2,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(composite,x + getWidth(img1) - 100,y),color)
  return composite
This results in the following image:

To improve this, you are asked to blend the two source images with varying weights, i.e. weights that vary over the region of overlap. In particular, at the left boundary of the overlap region, the left image should have 100% weight, whereas the right image should have 0%. Conversely, at the right boundary of the overlap region, the left image should have 0% weight, whereas the right image should have 100%. In between the weights should vary linearly.

One simple way to generate these varying weights is to compute a gradient, see e.g. the greyscale gradient example in section 5.5. Your task for this lab is to modify this function so that it arranges the two images side by side, where the size of the overlap is specified as an argument to the function. The overlapping area must be blended, as in the second image. The function must be named blendimages(file1,file2,overlap). The arguments are the name of the first and second image file, respectively, and the overlap area in pixels. The function must return a composite image that contains the blended result.

Lab Questions and What to Turn in

As mentioned previously, you have to add comments with your own identification (name, student ID, date). Remember to save the file after you modify it!

How to submit the lab

Exactly like in lab02, you hand in this lab the submit command on the Linux systems that are behind the PRISM lab infrastructure. For details on how to submit, please refer to lab02. However, this time please submit to lab05, i.e. issue the command

submit 1710 lab05 lab05.py

Note: You must do all the above steps correctly for receiving full credit for this lab.