2013-07-05 3 views
1

그림의 일부 (이 예에서는 코)의 크기를 조정하고 싶습니다.그림의 일부분 조정하기

사진의 원하는 부분을 선택하여 확대하고 싶습니다.

def Enlarge(picture): 
    height = getHeight(picture) 
    width = getWidth(picture) 
    newPicture = makeEmptyPicture(width*2, height*2) 
    x1=0 
    for x in range(0,width): 
    y1=0 
    for y in range(0,height): 
     pxl = getPixel(picture,x,y) 
     newPxl = getPixel(newPicture, x1,y1) 
     color = getColor(pxl) 
     setColor(newPxl,color) 

     y1=y1+2 
    x1=x1+2 

    return newPicture 

예 :

Copy paste nose

def copyAndPaste(picture): 
    height = getHeight(picture) 
    width = getWidth(picture) 
    newPicture = makeEmptyPicture(width, height) 
    for x in range(width): 
    for y in range(height): 
     pxl = getPixel(picture,x,y) 
     if (x>48 and x<59) and (y>58 and y<71): 
     newPxl =getPixel(newPicture, #?,#?) 
     else: 
     newPxl = getPixel(newPicture, x,y) 
     color = getColor(pxl) 
     setColor(newPxl,color) 

    return newPicture 

def d():  
    f=pickAFile() 
    picture=makePicture(f)   
    newPicture = copyAndPaste(picture)   
    writePictureTo(newPicture, r"D:\FOLDER\0Pic4.jpg") 
    explore (newPicture) 

나는 또한 사진을 확대하는 기능을 가지고있다.

Original Pic

사람 : 보낸 사람 :

Enlarged Pic

나는 나머지를 떠나, 많은 것들을 시도하지만, 사진의 일부를 확대하려면 두 가지를 결합하는 방법을 작동하지 않을 수 있습니다 재치있는 그림.

(이것은만큼 우스꽝를) 프로그램을 실행하는 데 너무 오래 걸릴 수있는 결과 사진, 같은 나는 작은 이미지에 연습 한

enlarged nose

을 보일 것입니다 것입니다, 그것은이다 이 단계에서는 큰 이미지로 작업 할 수 없으므로 결과가 개략적이지만 실제 작동하는 경우 표시됩니다.

+0

첫 번째 기능은 원본보다 양방향으로 100px 큰 그림으로 시작하여 일부분의 픽셀을 새 부분의 동일한 부분으로 복사하는 것, 두 번째 기능은 1/새로운 4 개의 픽셀. 이게 당신이 의도 한 것입니까? –

+0

샘플 이미지에 연결하고'copy45'와'copy48' (그 기괴한 이름은 무엇입니까?), 그리고 새로운 기능을 무엇을하고 싶은지를 보여주는 것이 도움이 될 것입니다. 왜냐하면 나는 당신의 설명에서 그것을 이해할 수 없기 때문에 (코드는 첫 번째 경우에 "자르기"하지 않고 두 번째 경우에는 매우 이상한 방식으로 "확대"합니다). – abarnert

+0

@Yve :이 질문은 사진이 없으면 분명하지 않았고, 내가 요청한 세 번째 사진이 없으면 여전히 명확하지 않습니다. 실제로 어떤 일이 일어나길 원하는지 보여줍니다. 그렇지 않으면, 우리는 단지 당신이하려고하는 것을 추측해야만합니다. 누군가가 당신을 돕기 전에 여러 가지 나쁜 추측을 거쳐야 할 것입니다. 그것은 당신만큼 성가신 것입니다. – abarnert

답변

4

난 아직도 당신이 뭘하려는 건지 이해가 안 확신 해요,하지만 난 그것을 이런 식으로 뭔가 생각 : 당신은 복사 할 대신 컷의 코를 붙여을 붙여 원하는 붙여 넣은 사본은 두 번째 예제와 동일한 방식으로 두 배가됩니다.

그래서 얼굴 중앙에는 10x10의 코가 있고 오른쪽 하단에는 20x20의 씻어 내린 코가 있습니다.


복사, 붙여 넣기, 당신은 단지 대신 만의 새로운 위치로 이전 및 새 위치로 픽셀을 복사해야 할 첫 번째 : 이제

def copyAndPaste(picture): 
    height = getHeight(picture) 
    width = getWidth(picture) 
    newPicture = makeEmptyPicture(width+100, height+100) 
    for x in range(width): 
    for y in range(height): 
     pxl = getPixel(picture,x,y) 
     color = getColor(pxl) 
     if (x>48 and x<59) and (y>58 and y<71): 
     newPxl =getPixel(newPicture, x+100,y+100) 
     setColor(newPxl,color) 
     newPxl = getPixel(newPicture, x,y) 
     setColor(newPxl,color) 

의] 새로 확대하기 붙여 넣기 한 복사본은 오프셋을 두 배로 늘리면됩니다. 즉, 49,59의 첫 번째 픽셀은 149,159로 이동하지만 50,60의 픽셀은 151,161으로 이동하고 51,61의 픽셀은 153,163으로 이동합니다.

 if (x>48 and x<59) and (y>58 and y<71): 
     newPxl =getPixel(newPicture, (x-49)*2+49+100,(y-59)*2+59+100) 
     setColor(newPxl,color) 
+1

동일한 픽셀을 4 번 복사하는 대신 모든 복사 된 픽셀에 대해 3 개의 흰색 픽셀을 남기고 싶습니다. (그래서 씻겨 나가지 않고 원본처럼 보입니다.) 왜냐하면 제가 학생에게 "이 이미지 확대"를 요청하고 그것이 그들이 제출 한 것이기 때문에 나는 그들에게 아주 좋은 성적을주지 않을 것입니다. (그리고 제가 학생이었고 선생님이 저를 돌리기를 원했을 때 나는 Python을 배우기위한 더 좋은 방법을 찾고 테스트를 위해서만 수업을 보았습니다.) – abarnert

2

이 기록을 위해 단지이다

그럼, 당신이 원하는 것은 100,100으로 이동 한 후, 그것을 두 번, 49,59에서 거리를 얻을 49,59에 다시 추가하는 것입니다 와 재미를 위해,없는 대답 ...

그러나 스케일링 알고리즘으로 아주 터무니 인 abarnert ("Are you sure they just want you to leave 3 white pixels for every copied pixel, rather than copying the same pixel 4 times?") 언급 한 바와 같이

...

많은 흥미 아직 기본적인 접근 방식은 이미지가있다 확장 할 수 Nearest Neighbor Algorithm.


def EnlargeNearestNeighbor(picture, multiplier): 

    w1 = getWidth(picture) 
    h1 = getHeight(picture) 
    w2 = getWidth(picture) * multiplier 
    h2 = getHeight(picture) * multiplier 

    x_ratio = w1/float(w2) 
    y_ratio = h1/float(h2) 

    newPicture = makeEmptyPicture(w2, h2) 

    for x in range(0, w2): 
    for y in range(0, h2): 
     newPx = getPixel(newPicture, x, y) 

     px = floor(x*x_ratio); 
     py = floor(y*y_ratio); 

     oldPx = getPixel(picture, int(px), int(py)) 
     setColor(newPx, getColor(oldPx)) 

    return newPicture 

file = pickAFile() 
picture = makePicture(file) 
pic = EnlargeEagle(picture) 

pic2 = EnlargeNearestNeighbor(picture, 3) 

은 ............................................. ..... enter image description here .......................................... enter image description here. ..........................................


다른 흥미로운 알고리즘 here. 여기


는 (다른 색상의 낮은 숫자로 이미지와 함께 잘 작동)을 Eagle Algorithm의 기본 구현 : 원래

def EnlargeEagle(picture): 

    w = getWidth(picture) 
    h = getHeight(picture) 
    w2 = getWidth(picture)*2 
    h2 = getHeight(picture)*2 

    newPicture = makeEmptyPicture(w2, h2) 

    x2 = 0 
    for x in range(1, w-1): 
    y2 = 0 
    for y in range(1, h-1): 

     oldPxS = getPixel(picture, x-1, y-1) 
     oldPxT = getPixel(picture, x, y-1) 
     oldPxU = getPixel(picture, x+1, y-1) 

     oldPxV = getPixel(picture, x-1, y) 
     oldPxC = getPixel(picture, x, y) 
     oldPxW = getPixel(picture, x+1, y) 

     oldPxX = getPixel(picture, x-1, y+1) 
     oldPxY = getPixel(picture, x, y+1) 
     oldPxZ = getPixel(picture, x+1, y+1) 

     newPx1 = getPixel(newPicture, x2, y2) 
     newPx2 = getPixel(newPicture, x2+1, y2) 
     newPx3 = getPixel(newPicture, x2, y2+1) 
     newPx4 = getPixel(newPicture, x2+1, y2+1) 

     # Step 1 
     c = getColor(oldPxC) 
     setColor(newPx1, c) 
     setColor(newPx2, c) 
     setColor(newPx3, c) 
     setColor(newPx4, c) 

     # Step 2  
     if (getColor(oldPxV) == getColor(oldPxS)) and (getColor(oldPxS) == getColor(oldPxT)): 
     setColor(newPx1, getColor(oldPxS)) 

     if (getColor(oldPxT) == getColor(oldPxU)) and (getColor(oldPxU) == getColor(oldPxW)): 
     setColor(newPx2, getColor(oldPxU)) 

     if (getColor(oldPxV) == getColor(oldPxX)) and (getColor(oldPxX) == getColor(oldPxY)): 
     setColor(newPx3, getColor(oldPxX)) 

     if (getColor(oldPxW) == getColor(oldPxZ)) and (getColor(oldPxZ) == getColor(oldPxY)): 
     setColor(newPx4, getColor(oldPxZ)) 



    y2 += 2 
x2 += 2 

:

enter image description here

가장 가까운 이웃 :,973,210

enter image description here

이글 :

enter image description here


즐기십시오!