2013-10-12 36 views
-1

嘿,我必須創建一個函數來創建一個垂直於原始圖片的鏡像的新圖片。在Jython/Python上的垂直鏡像圖像

因此,原始圖片的左上角將成爲新圖片的右上角,而原稿的左下角將成爲新圖片的右下角。我是新來的編程,所以我很抱歉,這個殘酷的事情......我用參數「pic」創建了一個函數,它是一張圖片,複製它並創建「新」,並創建一個名爲「畫布「與」圖片「具有相同的尺寸。我首先使用我的函數「pic」和「new」做了兩個半鏡像,然後我嘗試將相應的半部分複製/連接到我的空畫布(拼貼功能)。

我得到它的畫布有我需要的前半部分,但當我嘗試添加第二個程序崩潰,並給我>錯誤...「getPixel(picture,x,y ):X(= 466)小於0或大於寬度(= 465)更大

錯誤是:

Inappropriate argument value (of correct type). An error occurred attempting 
to pass an argument to a function. 

我已經嘗試了在45分鐘內以1玩弄,-1 ,-2等等,無處不在,但無法使用>我的代碼如下所示:

def mirrorMe(pic): 
    mirrorPoint = getWidth(pic)/2 
    width = getWidth(pic) 
    new = duplicatePicture(pic) 
    canvas = makeEmptyPicture(getWidth(pic), getHeight(pic)) 
    widthb = getWidth(new) 
    mirrorPointb = getWidth(new)/2 
    for y in range(0,getHeight(pic)): 
     for x in range(0,mirrorPoint): 
       leftPixel = getPixel(pic,x,y) 
       rightPixel = getPixel(pic,width - x - 1,y) 
       color = getColor(leftPixel) 
       setColor(rightPixel,color) 
       #show(pic) 
       newwpic = getWidth(pic) 
       newhpic = getHeight(pic) 
    for y in range(0,getHeight(new)): 
     for x in range(0,mirrorPointb): 
      leftPixel = getPixel(new,x,y) 
      rightPixel = getPixel(new,widthb - x - 1,y) 
      color = getColor(rightPixel) 
      setColor(leftPixel,color) 
      #show(new) 
      newwnew = getWidth(new) 
      newhnew = getHeight(new) 
    for x in range(0, newwnew/2): 
     for y in range(0,newhnew): 
      setColor(getPixel(canvas, x, y), getColor(getPixel(new, x, y))) 
explore(canvas) 
#show(canvas) 
#print (newwpic/2)-(newwpic) 
    for xx in range((newwpic/2), newwpic): 
     for y in range(0,newhpic): 
      pixel = getPixel(pic, xx, y) 
      color = getColor(pixel) 
      targetPixelz = getPixel(canvas, xx+(newwpic/2)-1, y) 
      setColor(targetPixelz, color) 
explore(canvas) 
show(canvas) 

回答

0

您遇到的錯誤是針對索引錯​​誤: getPixel(圖片,x,y):x(= 466)小於0或大於寬度(= 465) 即您可以提供的最大x在getPixel的圖片是465

我會建議解決圖片翻轉使用x上的反向範圍爲新的圖片上的每個y。如下所示:

# Assuming you have orig_pic and new_pic(empty) 
width = getWidth(new_pic)-1 
for y in range(0,getHeight(orig_pic)): 
    for x in range(getWidth(orig_pic): 
     leftPixel = getPixel(orig_pic, x, y) 
     rightPixel = getPixel(orig_pic, width-x, y) 
     leftColor = getColor(leftPixel) 
     rightColor = getColor(rightPixel) 
     setColor(getPixel(new_pic, width-x, y), leftColor) 
     setColor(getPixel(new_pic, x, y), rightColor) 

這一個循環應該能夠以垂直翻轉的方式將原始圖像複製到新的圖像畫布。 new_pic現在有你想要的圖像。