首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将图像像素值从rgb转换为灰度,手动python?

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

Stack Overflow用户
提问于 2015-05-01 14:43:46
回答 3查看 2.6K关注 0票数 3

我试图使用特定伽马校正灰度的实现 - Gleam将图像像素转换为灰度.如何使用PIL python手动完成此操作?

代码语言:javascript
复制
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元组,我认为。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-05-01 15:28:18

我发现我可以建立另一个形象:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2015-05-01 16:57:47

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

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

票数 1
EN

Stack Overflow用户

发布于 2015-05-01 15:05:25

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

代码语言:javascript
复制
sample_tuple = (1, 2, 3, 4)

因此,我们将gleam()函数编辑为:

代码语言:javascript
复制
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, )

请记住,在返回单个元素元组时,需要将其表示为:

代码语言:javascript
复制
sample_tuple = (1, )

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29989024

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档