2015-05-01 102 views
1

我試圖使用specific gamma corrected grayscale implementation - 微光將圖像像素轉換爲灰度。我怎樣才能用PIL python手動執行此操作?手動將圖像像素值從rgb轉換爲灰度python PIL?

def tau_gamma_correct(pixel_channel): 
    pixel_channel = pixel_channel**(1/2.2) 
    return pixel_channel 

#@param: rgb 
#@result: returns grayscale value 
def gleam(rgb): 
    #convert rgb tuple to list 
    rgblist = list(rgb) 
    #gamma correct each rgb channel 
    rgblist[0] = tau_gamma_correct(rgblist[0]) 
    rgblist[1] = tau_gamma_correct(rgblist[1]) 
    rgblist[2] = tau_gamma_correct(rgblist[2]) 
    grayscale = 1/3*(rgblist[0] + rgblist[1] + rgblist[2]) 
    return grayscale 

# get a glob list of jpg filenames 
files = glob.glob('*.jpg') 
for file in files: 
    file = open(file) 
    filename = file.name 
    image = Image.open(file) 
    pix = image.load() 
    width, height = image.size 
    #print(width,height) 
    for x in range(0, width): 
     for y in range(0, height): 
      rgb = pix[x,y] 
      #print(rgb) 
      # calc new pixel value and set to pixel 
      image.mode = 'L' 
      pix[x,y] = gleam(rgb) 

      image.save(filename + 'gray.gleam'+'.jpg') 
    file.close() 

SystemError: new style getargs format but argument is not a tuple
它仍然期待RGB元組,我認爲。

+0

嘗試移動'file.close()'2縮進塊,並看到它解決了這個問題?我的意思是移動'file.close()'命令,就像'open()'方法一樣在縮進級別。 – ZdaR

+0

沒有幫助,問題是我不知道如何將rgb元組更改爲單個灰度值。即時嘗試改變模式,但它似乎不工作,或者它不能被改變。我會檢查PIL文檔。 – jsky

+0

不是'gleam'函數返回0到255之間的整數灰度值嗎?你想要一個由函數返回的元組而不是整數嗎? – ZdaR

回答

0

我發現我可以只建一個像這樣的事實:

import sys 
import os 
import glob 
import numpy 
from PIL import Image 

def tau_gamma_correct(pixel_channel): 
    pixel_channel = pixel_channel**(1/2.2) 
    return pixel_channel 

#@param: rgb 
#@result: returns grayscale value 
def gleam(rgb): 
    #convert rgb tuple to list 
    rgblist = list(rgb) 
    #gamma correct each rgb channel 
    rgblist[0] = tau_gamma_correct(rgblist[0]) 
    print('gleamed red ' + str(rgblist[0])) 
    rgblist[1] = tau_gamma_correct(rgblist[1]) 
    print('gleamed green ' + str(rgblist[1])) 
    rgblist[2] = tau_gamma_correct(rgblist[2]) 
    print('gleamed blue ' + str(rgblist[0])) 
    grayscale = (rgblist[0] + rgblist[1] + rgblist[2])/3 
    print('grayscale '+ str(grayscale)) 
    return grayscale 

# get a glob list of jpg filenames 
files = glob.glob('*.jpg') 
for file in files: 
    file = open(file) 
    filename = file.name 
    image = Image.open(file) 
    pix = image.load() 
    width, height = image.size 
    new_image = Image.new('L', image.size) 
    #pixelmatrix = [width][height] 
    pixelmatrix = numpy.zeros((width, height)) 
    #print(width,height) 
    for x in range(0, width): 
     for y in range(0, height): 
      rgb = pix[x,y] 
      print('current pixel value: '+str(rgb)) 
      # calc new pixel value and set to pixel 
      #print(gleam(rgb)) 
      gray = gleam(rgb) 
      print('changing to pixel value: '+str(gray)) 
      pixelmatrix[x,y] = gray 
      new_image.save(filename + 'gray.gleam'+'.jpg') 
    new_image.putdata(pixelmatrix) 
    file.close() 
0

眼看SystemError: new style getargs format but argument is not a tuple錯誤看來,你需要返回一個元組,其表示爲:

sample_tuple = (1, 2, 3, 4) 

所以我們編輯gleam()功能:

def gleam(rgb): 
    #convert rgb tuple to list 
    rgblist = list(rgb) 
    #gamma correct each rgb channel 
    rgblist[0] = tau_gamma_correct(rgblist[0]) 
    rgblist[1] = tau_gamma_correct(rgblist[1]) 
    rgblist[2] = tau_gamma_correct(rgblist[2]) 
    grayscale = 1/3*(rgblist[0] + rgblist[1] + rgblist[2]) 
    return (grayscale,) 

請記住,雖然返回單個元素元組,您需要表示爲:

sample_tuple = (1,) 

這是由於(4) == 4(4,) != 4

0

的問題是,image.mode = 'L'實際上並不只是改變圖像的類型,改變屬性,所以它不再準確。要更改圖像的模式,您需要使用image.convert('L')製作新副本。

一旦你在灰度模式下的圖像,它不會再需要一個元組的像素值。

+0

謝謝。這似乎是我需要處理每個像素,它[對我來說足夠好]從頭開始構建灰度。即。我不需要轉換原來的,我只需要灰度。在將numpy數組/矩陣數據放入圖像中時遇到問題。 – jsky

+0

http://pastebin.com/6QpsKeqx – jsky

+0

@jsky你有兩個對象'new_image'和'new_img',你可能會讓他們感到困惑。 –