2013-03-28 35 views
2

我的目標是將圖片大小加倍,然後將左半部分更改爲灰階,然後更改右上半部分的綠色值和右下半部分的藍色值。我有我在我的教科書中發現的灰度值,但我不確定這是否是我實際使用的值。而且我也不能確定,如果我使用循環或只是一些不同的程序每一個不同的值如何使用JES編程更改圖像顏色?

到目前爲止,我的代碼是:

def crazyPic(newGreen,newBlue,pic,file): 
     show(pic) 
     newPic = makeEmptyPicture(getWidth(pic)*2,getHeight((pic)*2 
      for x in range(width): 
       for y in range(height): 
        for px in getPixel(pic,0,100): 
        nRed = getRed(px) * 0.299 
        nGreen = getGreen(px) * 0.587 
        nBlue = getBlue(px) * 0.114 
        luminance = nRed + nGreen + nBlue 
        setColor(px,makeColor(luminance,luminance,luminance) 

回答

0

我不應該充分答案爲JES是一個應用程序專爲學生,但我認爲三個月後可以給它可以用作其他參考完整工作示例...

這應該是接近你試圖做什麼:

注意:您對x和y的簡單雙重循環方法是正確的。

def crazyPic(pic, newRed, newGreen, newBlue): 

    w = getWidth(pic) 
    h = getHeight(pic) 
    new_w = w * 2 
    new_h = h * 2 
    newPic = makeEmptyPicture(w * 2, h * 2) 

    for x in range(new_w): 
     for y in range(new_h): 
      new_px = getPixel(newPic, x, y) 

      # Top-left: B&W 
      if (x < w) and (y < h): 
      px = getPixel(pic, x, y) 
      nRed = getRed(px) * newRed #0.299 
      nGreen = getGreen(px) * newGreen #0.587 
      nBlue = getBlue(px) * newBlue #0.114 
      luminance = nRed + nGreen + nBlue 
      new_col = makeColor(luminance, luminance, luminance) 

      # Top-right 
      elif (y < h): 
      px = getPixel(pic, x - w, y) 
      nRed = getRed(px) * newRed 
      new_col = makeColor(nRed, getGreen(px), getBlue(px)) 

      # Bottom-left 
      elif (x < w): 
      px = getPixel(pic, x, y - h) 
      nGreen = getGreen(px) * newGreen 
      new_col = makeColor(getGreen(px), nGreen, getBlue(px)) 

      # Bottom-right 
      else: 
      px = getPixel(pic, x - w, y - h) 
      nBlue = getBlue(px) * newBlue 
      new_col = makeColor(getGreen(px), getBlue(px), nBlue) 

      setColor(new_px, new_col) 

    return newPic 

file = pickAFile() 
picture = makePicture(file) 
#picture = crazyPic(picture, 0.299, 0.587, 0.114) 
# Here, with my favorite r, g, b weights 
picture = crazyPic(picture, 0.21, 0.71, 0.07) 

writePictureTo(picture, "/home/quartered.jpg") 

show(picture) 


輸出(繪畫由安東尼塔皮埃斯):


enter image description here ......來自...... enter image description here


這裏是更多detailed thread灰度