我一直收到一个错误,说有没有图片。我究竟做错了什么?

时间:2016-12-17 02:59:30

标签: jython jes

def blendPictures(pict1, pict2, overlapAmt):
  width1 = getWidth(pict1)
  height1 = getHeight(pict1)
  width2 = getWidth(pict2)
  height2 = getHeight(pict2)

  newWidth = width1 + width2 - overlapAmt
  newHeight = min(height1, height2)

  newCanvas = makeEmptyPicture(newWidth, newHeight)

  for x in range(width1 - overlapAmt):
    for y in range(newHeight): 
      color = getColor(getPixel(pict1, x, y))
      setColor(getPixel(newCanvas, x, y), color)


  pict2_x = 0
  for pict1_x in range(width1 - overlapAmt, width1):
    for y in range(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


  targetX = width1
  for x in range(overlapAmt, width2):
    for y in range(newHeight):
      color = getColor(getPixel(pict2, x, y))
      setColor(getPixel(newCanvas, targetX, y), color)
    targetX = targetX + 1


  return newCanvas


def swapBackground( src, background, newBackground ):
    # src, and background must be the same size
    # newBackground must be at least as big as src and background
    for x in range(1, getWidth( src ) + 1 ) :
              for y in range(1, getHeight( src ) + 1 ) :
        srcPxl = getPixel( src, x, y )
            backgroundPxl = getPixel( background, x, y )
            if (distance(getColor( srcPxl ),  getColor( backgroundPxl )) < 15.0):
                setColor( srcPxl, getColor( getPixel( newBackground, x, y )  )  )
    return src


jackalope = blendPictures('rabbit.jpg', 'antelope.jpg', 50)
writePictureTo(jackalope, "./jackalope.jpg")
#here swap the background to place your photo on front
swapBackground( 'photograph.jpg', 'jackalope.jpg', newBackground )
writePictureTo(newBackground, "./nexttojackalope.jpg")


swapBackground( 'rabbit.jpg', 'campus.jpg', newBackground1 )
writePictureTo(newBackground1, "./campustemp.jpg")
swapBackground( 'antelope.jpg', 'campustemp.jpg', newBackground2 )
writePictureTo(newBackground1, "./campusfinal.jpg")

我将.jpg放在文件将放置的位置或放置的位置。我有一张兔子和一只带有jackalope wav文件的羚羊的照片,还有一张我自己的照片和一张照片作为背景。但我仍然在第2行得到错误,说getWidth(图片)没有定义,我不知道该怎么做。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

为了使用getWidth()函数,您需要传递图片对象作为其参数。因此,您需要首先使用makePicture()函数创建图片对象。例如:

pict1 = makePicture('rabbit.jpg')
width1 = getWidth1(pict1)

这是在JES中处理图片的好指南。 http://www.cs.bu.edu/courses/cs101b1/jes/#Pictures%20and%20Sound