我試圖使用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元組,我認爲。
嘗試移動'file.close()'2縮進塊,並看到它解決了這個問題?我的意思是移動'file.close()'命令,就像'open()'方法一樣在縮進級別。 – ZdaR
沒有幫助,問題是我不知道如何將rgb元組更改爲單個灰度值。即時嘗試改變模式,但它似乎不工作,或者它不能被改變。我會檢查PIL文檔。 – jsky
不是'gleam'函數返回0到255之間的整數灰度值嗎?你想要一個由函數返回的元組而不是整數嗎? – ZdaR