[Image-SIG] Colorize Image

jcupitt at gmail.com jcupitt at gmail.com
Thu Feb 11 12:50:11 CET 2010


On 10 February 2010 11:01, Espen Moe-Nilssen <espen at medialog.no> wrote:
> We are making an open source project, http://plone.org/products/subskins
> I have been trying over and over again to find a way to let PIL colorize an
> image.
> It should work like this:
> 1) I have a greyscale image
> 2) A color is selected, for example "#123456"
> 3) The image is colorized in this way:
>     Whatever was white is still white
>     Whatever was black (100%) is now "#123456"
>     Whaterver was grey (50%) is now 50% of "#123456"
> I have tried a lot of different approaches, but the colors always come out
> too dark. Is there a way to do this ?

You need to split the image into luminance (your greyscale image) and
colour (the constant colour you want to apply).

If you imagine the RGB space as a cube, the neutral axis (all the grey
colours) is a straight line from (0, 0, 0) to (255, 255, 255). What
you want to do is translate that line (ie. keep the angle of the thing
the same, just translate the whole line up/down/left/right/fwd/back)
so that it passes through #123456 instead. Any bits that go over 255
or under 0 need to be clipped.

This is easy in CIELAB space, but still OK in RGB.

John


More information about the Image-SIG mailing list