我正在尝试使用特定伽马校正灰度实现 - 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()
系统错误:新样式的getargs格式,但参数不是元组
我认为它仍然期望rgb元组。
file.close()
命令向前移动 2 个缩进块,看看是否可以解决问题?我的意思是将file.close()
命令与open()
方法在同一缩进级别。 - ZdaRgleam
函数不是返回介于0和255之间的整数灰度值吗?您是否想让该函数返回元组而不是整数? - ZdaR