【发布时间】:2015-07-11 09:53:50
【问题描述】:
我正在尝试使用 specific gamma corrected grayscale implementation - Gleam 将图像像素转换为灰度。如何使用 PIL python 手动执行此操作?
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()方法相同的缩进级别。 -
没有帮助,问题是我不确定如何将 rgb 元组更改为单个灰度值。我试图改变模式,但它似乎不起作用,或者它可能无法改变。我会检查 PIL 文档。
-
gleam函数不是返回 0 到 255 之间的整数灰度值吗?你想要一个由函数返回的元组而不是整数吗? -
是的,这就是我的目标..
-
我想将像素值设置为灰度值而不是 rgb 元组
标签: python python-imaging-library grayscale