Image processing Python:想像photoshop那样更改图像HSL吗

Image processing Python:想像photoshop那样更改图像HSL吗,image-processing,python-imaging-library,Image Processing,Python Imaging Library,我想在Python中实现这个特性(用勾选的颜色更改HSL),最好使用PIL或numpy 有人能解释一下这是怎么回事吗 据我所知,是使用内置函数color_to_hsl来获取hsl值,更改它,然后将ti转换回rgb,最后写入单个像素 有什么线索可以靠近它吗 from PIL import Image import colorsys def colorize(im, h, s, l_adjust): h /= 360.0 s /= 100.0 l_adjust /= 10

我想在Python中实现这个特性(用勾选的颜色更改HSL),最好使用PIL或numpy

有人能解释一下这是怎么回事吗

据我所知,是使用内置函数color_to_hsl来获取hsl值,更改它,然后将ti转换回rgb,最后写入单个像素

有什么线索可以靠近它吗

from PIL import Image
import colorsys

def colorize(im, h, s, l_adjust):
    h /= 360.0
    s /= 100.0
    l_adjust /= 100.0
    if im.mode != 'L':
        im = im.convert('L')
    result = Image.new('RGB', im.size)
    pixin = im.load()
    pixout = result.load()
    for y in range(im.size[1]):
        for x in range(im.size[0]):
            l = pixin[x, y] / 255.99
            l += l_adjust
            l = min(max(l, 0.0), 1.0)
            r, g, b = colorsys.hls_to_rgb(h, l, s)
            r, g, b = int(r * 255.99), int(g * 255.99), int(b * 255.99)
            pixout[x, y] = (r, g, b)
    return result

这正是您在photoshop中使用颜色检查所做的

from PIL import Image
import colorsys

def rgbLuminance(r, g, b):
    luminanceR = 0.22248840
    luminanceG = 0.71690369
    luminanceB = 0.06060791
    return (r * luminanceR) + (g * luminanceG) + (b * luminanceB)


def colorize(im, h, s, l_adjust):
    h /= 360.0
    s /= 100.0
    l_adjust /= 100.0
    result = Image.new('RGBA', im.size)
    pixin = im.load()
    pixout = result.load()
    for y in range(im.size[1]):
        for x in range(im.size[0]):
            currentR = pixin[x, y][0]/255
            currentG = pixin[x, y][1]/255
            currentB = pixin[x, y][2]/255
            lum = rgbLuminance(currentR, currentG, currentB)
            if l_adjust > 0:
                lum = lum * (1 - l_adjust)
                lum = lum + (1.0 - (1.0 - l_adjust))
            else:
                lum = lum * (l_adjust + 1)
            l = lum
            r, g, b = colorsys.hls_to_rgb(h, l, s)
            r, g, b = int(r * 255.99), int(g * 255.99), int(b * 255.99)
            pixout[x, y] = (r, g, b, 255)
    return result

选中“着色”框后,我认为您正在使用原始图像中的灰度值作为亮度,并直接设置色调和饱和度,如对话框中所示。很抱歉,我没有时间留下答案,这真的很简单。相关人员:非常感谢!我是图像处理的初学者,将来我需要做更多的图像处理。你介意分享一些这方面的链接/书籍吗?@user469652,我不确定我能帮上什么忙,因为这些年来我只是零零碎碎地收集了一些教材。不过我很好奇,因为我没有Photoshop,我的例子和它的结果相符吗?