How to recolor grayscale image with fixed colors by ImageMagick using CLUT

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
cagdabr
Posts: 2
Joined: 2017-09-08T02:13:22-07:00
Authentication code: 1151

How to recolor grayscale image with fixed colors by ImageMagick using CLUT

Post by cagdabr » 2017-09-08T06:33:25-07:00

I'm pretty new to image processing and ImageMagick, but I couldn't find a straightforward solution to my problem after some searching. I still don't have an understanding how the Color LookUp Table [CLUT] works in ImageMagick.

I have source image:
Image

And I want it look like this:
Image

The idea is that in source image I have gray values which should be converted to color values 1:1

Code: Select all

rgb(0, 0, 0)  -> transparent
rgb(1, 1, 1)  -> rgb(253,0,252)
rgb(2, 2, 2)  -> rgb(252,0,23)
rgb(3, 3, 3)  -> rgb(253,125,33)
rgb(4, 4, 4)  -> rgb(254,217,48)
rgb(5, 5, 5)  -> rgb(219, 255, 51)
rgb(6, 6, 6)  -> rgb(59, 255, 46)
rgb(7, 7, 7)  -> rgb(110, 165, 58)
rgb(8, 8, 8)  -> rgb(18, 139, 54)
rgb(9, 9, 9)  -> rgb(44, 255, 254)
rgb(10,10,10) -> rgb(18, 129, 252)
rgb(11,11,11) -> rgb(39, 19, 251)
rgb(12,12,12) -> rgb(115, 115, 115)
rgb(13,13,13) -> rgb(179,179,179)
But I don't know how to create CLUT image in order to make ImageMagick convert it as I want. I tried to create something like this:

Image

... but when I do:

Code: Select all

$> convert region_128x128.png color_lookup_table.png -clut region_128x128_clut.png
I'm getting this:

Image

So, the question is, Is it possible to create the CLUT for my problem? If so, could someone please explain it to me?

snibgo
Posts: 9404
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to recolor grayscale image with fixed colors by ImageMagick using CLUT

Post by snibgo » 2017-09-08T07:39:39-07:00

This does the job. Windows BAT syntax.

Code: Select all

convert ^
  jLHjz.png ^
  -auto-level ^
  ( -size 1x1 ^
    xc:rgb(253,0,252) ^
    xc:rgb(252,0,23) ^
    xc:rgb(253,125,33) ^
    xc:rgb(254,217,48) ^
    xc:rgb(219,255,46) ^
    +append ^
  ) ^
  -clut ^
  out.png
Your input has only 5 shades of gray, from 1 to 5 out of 255, so I auto-levelled to widen the range to 0 to 100%, then supplied a clut with 5 values.

Instead, I could have omitted the auto-level, and supplied a clut with 256 values.
snibgo's IM pages: im.snibgo.com

cagdabr
Posts: 2
Joined: 2017-09-08T02:13:22-07:00
Authentication code: 1151

Re: How to recolor grayscale image with fixed colors by ImageMagick using CLUT

Post by cagdabr » 2017-09-13T06:40:48-07:00

snibgo wrote:
2017-09-08T07:39:39-07:00
...
Your input has only 5 shades of gray, from 1 to 5 out of 255, so I auto-levelled to widen the range to 0 to 100%, then supplied a clut with 5 values.

Instead, I could have omitted the auto-level, and supplied a clut with 256 values.
Thank you for your reply @snibgo. This particular source image may not have all gray values but clut should be able to handle the rest of them in case if they will be in the image.

Post Reply