# Recipe 41: Blend two pictures

def blendPictures():
  pic1 = makePicture(getMediaPath("thailand-06.jpg"))
  pic2 = makePicture(getMediaPath("thailand-07.jpg"))
  # The size of both pictures is 533 x 400 pixels

  canvas = makeEmptyPicture(1000,500)

  # Step 1: Copy first 400 columns of picture 1 (with big boat)
  sourceX = 1
  for targetX in range(1, 450):
    sourceY = 1
    for targetY in range(1,getHeight(pic1)+1):
      color = getColor(getPixel(pic1, sourceX, sourceY))
      setColor(getPixel(canvas, targetX, targetY + 50), color)
      sourceY = sourceY + 1
    sourceX = sourceX + 1

  # Step 2: Copy the overlapping (blending) columns
  overlap = getWidth(pic1) - 450
  sourceX = 1
  for targetX in range (450, getWidth(pic1)):
    sourceY = 1
    for targetY in range(1, getHeight(pic2)+1):
      pixelPic1 = getPixel(pic1, sourceX+450, sourceY)
      pixelPic2 = getPixel(pic2, sourceX, sourceY)
 
      newRed =   0.5*getRed(pixelPic1) + 0.5*getRed(pixelPic2)
      newGreen = 0.5*getGreen(pixelPic1) + 0.5*getGreen(pixelPic2)
      newBlue =  0.5*getBlue(pixelPic1) + 0.5*getBlue(pixelPic2)
      color = makeColor(newRed, newGreen, newBlue)
      setColor(getPixel(canvas, targetX, targetY), color)
      sourceY = sourceY + 1
    sourceX = sourceX + 1

  # Step 3: The rest of picture 2
  sourceX = overlap
  for targetX in range(450+overlap, 450+getWidth(pic2)):
    sourceY = 1
    for targetY in range(1,getHeight(pic1)+1):
      color = getColor(getPixel(pic2, sourceX, sourceY))
      setColor(getPixel(canvas, targetX, targetY), color)
      sourceY = sourceY + 1
    sourceX = sourceX + 1

  show(canvas)
  return canvas

