2015-06-01 82 views
2

嗨,我試圖玩弄我的代碼,並不能真正弄清楚如何將我的代碼的一部分變成灰度。這是我的代碼:如何在JES中創建圖片灰度的一部分?

def grayScale(picture): 
     xstop=getWidth(picture)/2 
     ystop=getHeight(picture)/2 
     for x in range(0,xstop): 
      for y in range(0,ystop): 
      oldpixel= getPixel(picture,x,y) 
      colour=getColor(oldpixel) 
      newColor=(getRed(oldpixel),getGreen(oldpixel),getBlue(oldpixel))/3 
      setColor(picture,(newColor,newColor,newColor)) 
     repaint(picture) 


nP=makePicture(pickAFile()) 
show(nP) 

任何幫助表示讚賞,真的很難明白這一點。再次感謝你的幫助!所示

錯誤:

灰度(NP) 錯誤是: '元組' 和 'INT'

不適當的參數類型。 嘗試使用無效類型的參數調用函數。這意味着你做了一些事情,比如試圖將一個字符串傳遞給一個期待整數的方法。 請檢查線路8 /用戶/ enochphan /桌面/測試

回答

1

有幾件事情給你的麻煩在這裏:你的代碼的

  • 縮進在y for循環後(我猜你想要遍歷所有的高度)。
  • 新顏色只是您當前像素的平均值,因此您需要使用加法才能在除以三之前添加它們。
  • setColor()需要一個像素和一個顏色對象。您想要更改的像素是oldpixel,並且使用makeColor()創建顏色對象。

這裏是代碼的所有修補程序實現的:

def grayScale(picture): 
     xstop=getWidth(picture)/2 
     ystop=getHeight(picture)/2 
     for x in range(0,xstop): 
      for y in range(0,ystop): 
      oldpixel= getPixel(picture,x,y) 
      colour=getColor(oldpixel) 
      newColor = (getRed(oldpixel)+getGreen(oldpixel)+getBlue(oldpixel))/3 
      setColor(oldpixel,makeColor(newColor,newColor,newColor)) 
     repaint(picture)