手动将图像像素值从 rgb 转换为灰度 python PIL?

Convert an images pixel value from rgb to grayscale manually python PIL?

我正在尝试使用 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元组。

看到 SystemError: new style getargs format but argument is not a tuple 错误,您似乎需要 return 一个元组,表示为:

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, )

请记住,当 return 使用单个元素元组时,您需要表示为:

sample_tuple = (1, )

这是因为 (4) == 4(4, ) != 4

我发现我可以构建另一个图像:

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()

问题是 image.mode = 'L' 实际上并没有改变图像的类型,它只是改变了属性,所以它不再准确。要更改图像模式,您需要使用 image.convert('L').

制作新副本

一旦您拥有灰度模式的图像,它就不再需要像素值的元组。