1

I would like to implement this feature(changing HSL with that colorize ticked) in Python, preferable using PIL or maybe numpy.

enter image description here

Can someone explain how this works?

As far as I know is to use the built-in function color_to_hsl to get the hsl value, change it, then convert ti back to rgb, and finally write to individual pixel.

Any clue to get make it closer?

user469652
  • 48,855
  • 59
  • 128
  • 165
  • With the Colorize box checked, I think you're working with the gray-scale values from the original image as Lightness and setting the Hue and Saturation directly as shown in the dialog. Sorry I don't have time to leave an answer, this is really quite simple though. – Mark Ransom Dec 26 '12 at 23:49
  • Related: http://stackoverflow.com/questions/6845374/how-to-colorize-via-hsl-colors/ – Ilmari Karonen Dec 26 '12 at 23:50

2 Answers2

3
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

Original Colorized

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • Much appreciated! I'm an beginner in image processing, and there will be more image processing I need to do in the future. Would you mind share some links/books in this area? – user469652 Dec 27 '12 at 11:55
  • @user469652, I'm not sure I can help with any teaching materials as I just picked it up piecemeal over the years. I'm curious though, since I don't have Photoshop does my example match what comes out of it? – Mark Ransom Dec 27 '12 at 18:02
  • @MarkRansom can you explain the - > min(max(l, 0.0), 1.0) – MSI Mar 02 '22 at 11:50
  • @MSI it's a simple way to make sure the value stays within bounds. `max(l, 0.0)` returns the original value if it's greater than `0.0`, or `0.0` if it isn't. Likewise `min(l, 1.0)` returns the original value if it's less than `1.0`, or `1.0` if it isn't. – Mark Ransom Mar 02 '22 at 12:57
1

This is what exactly you do in photoshop with colorize check

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
Amir Mahmoodi
  • 100
  • 1
  • 10