2013-06-14 3 views
2

그래서 이미지를 미러해야합니다. 이미지의 오른쪽 상단은 왼쪽 하단으로 뒤집어 져야합니다. 이미지의 왼쪽 상단을 오른쪽 아래로 뒤집는 기능을 만들었지 만, 나는 다른 방법으로 그것을 수행하는 방법을 알아낼 수 없습니다. 코드는 다음과 같습니다.자이 썬에서 대각선으로 이미지 미러링

def mirrorPicture(picture): 
height = getHeight(canvas) 
width = height 

# to make mirroring easier, let us make it a square with odd number 
# of rows and columns 
if (height % 2 == 0): 
    height = width = height -1 # let us make the height and width odd 


maxHeight = height - 1 
maxWidth = width - 1 

for y in range(0, maxWidth): 
     for x in range(0, maxHeight - y):  
     sourcePixel = getPixel(canvas, x, y) 
     targetPixel = getPixel(canvas, maxWidth - y, maxWidth - x) 
     color = getColor(sourcePixel) 
     setColor(targetPixel, color) 

return canvas 

btw, "JES"라는 IDE를 사용하고 있습니다. "미러링"에 의해, 당신은 "대각선 플립"을 의미하는 경우

+0

게시 된 코드에서 올바른 플립을 수행하지 않습니다. sourcePixel 및 targetPixel의 색상을 교환해야합니다. 귀하의 질문에 대해, 이런 식으로 생각 : 게시, 귀하의 프로그램 복사 (x, y) (max-y, max-x). 필요한 경우 다이어그램 그리기 : 어떤 픽셀을 다른 대각선을 따라 뒤집을 위치에 복사해야합니까? –

답변

2

이 작동합니다 :

def mirrorPicture(picture): 
    height = getHeight(picture) 
    width = getWidth(picture) 

    newPicture = makeEmptyPicture(height, width) 

    for x in range(0, width): 
     for y in range(0, height): 
      sourcePixel = getPixel(picture, x, y) 

      targetPixel = getPixel(newPicture, y, x) 
      #         ^^^^ (simply invert x and y) 
      color = getColor(sourcePixel) 
      setColor(targetPixel, color) 

    return newPicture 

주기 :


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


대각선으로 미러링에 대한 대답은 here입니다.

관련 문제