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

时间:2015-05-01 14:43:46

标签: python python-imaging-library grayscale

我尝试使用specific gamma corrected grayscale implementation - Gleam将图像像素转换为灰度。如何使用PIL python手动执行此操作?

SystemError: new style getargs format but argument is not a tuple

/* http://meyerweb.com/eric/tools/css/reset/ v2.0 | 20110126 License: none (public domain) */ html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video { margin: 0; padding: 0; border: 0; font-size: 100%; font: inherit; vertical-align: baseline; } /* HTML5 display-role reset for older browsers */ article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section { display: block; } body { line-height: 1; } ol, ul { list-style: none; } blockquote, q { quotes: none; } blockquote:before, blockquote:after, q:before, q:after { content: ''; content: none; } table { border-collapse: collapse; border-spacing: 0; }
它仍然期待着我认为的rgb元组。

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

图像处于灰度模式后,不再需要像素值的元组。