2016-12-17 7 views
0
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 파일과 자신의 그림과 배경으로 넣을 사진이있는 토끼와 영양 사진을 가지고 있습니다. 하지만 getWidth (picture)가 정의되어 있지 않고 무엇을해야할지 모르겠다는 2 행에서 여전히 오류가 발생합니다. 어떤 아이디어?나는 그림이 없다는 오류가 계속 발생합니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

+0

pict1 & pict2로 전달되는 항목은 무엇입니까? –

+0

코드를 볼 수 있도록 업데이트했습니다. 그리고 pict 1과 2는 무엇 이었습니까? – ThenewOne

답변

0

getWidth() 함수를 사용하려면 그림 개체를 인수로 전달해야합니다. 따라서 먼저 makePicture() 함수를 사용하여 그림 개체를 만들어야합니다. 예 :

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

다음은 JES에서 사진 작업을하는 데 유용한 가이드입니다. http://www.cs.bu.edu/courses/cs101b1/jes/#Pictures%20and%20Sound