Page 1 of 3

How use -clut option

Posted: 2014-04-05T09:47:25-07:00
by guillaumech
Hi!

I've some questions about clut option.
To sum up, I would like replace each color from image with clut option. First, I build an gray image with 7 colors

Code: Select all

( echo 'P2 32 22 7'; cat file; ) | convert pgm ...
The minimum value of the file is 0 and 7 maximum.
I obtain an gray image, like : Image

I resize this map with an another algorithm (hqx) for not create others values of color.
Result : Image

Now, I'd like colorize this map, with gradient level. I built the gradient with this code

Code: Select all

convert -size 8x2 xc: -draw '
      fill #FFFFFF rectangle    0,0 8,2
      fill #6EA1FF rectangle    1,0 8,2
      fill #3770DF rectangle    2,0 8,2
      fill #0040BF rectangle    3,0 8,2
      fill #FC85F8 rectangle    4,0 8,2
      fill #FD42F7 rectangle    5,0 8,2
      fill #FF00F7 rectangle    6,0 8,2
      fill #FA8484 rectangle    7,0 8,2' gradient.png
I would like replace the 0 value with the first gradient color (#FFFFFF), the 1 value with the second gradient color, etc.

I obtain none colors... I don't understand. It's a wrong way?
I don't want a gradient between two values, juste exactly the color of my gradient.
I searching a solution since 2 days :(

I noticed that when I was getting colors, it's not exactly the same that my gradient...

Thank you very much.
Guillaume.

Re: How use -clut option

Posted: 2014-04-05T10:20:16-07:00
by snibgo
Your small file, 01.png, has two colors: (0,0,0) and (68,68,68). A clut operation would look up only two colours in the clut file, at positions 0 and 68.

02.png has more colours, with pixel value from 0 to 68.

Your clut file has only 8 entries. It needs at least 69.

An easier way of creating a clut file with 8 entries is:

Code: Select all

convert xc:#FFFFFF xc:#6EA1FF xc:#3770DF xc:#0040BF xc:#FC85F8 xc:#FD42F7 xc:#FF00F7 xc:#FA8484 +append g2.png

Re: How use -clut option

Posted: 2014-04-05T10:32:26-07:00
by guillaumech
Hi snibgo!

Thank's
Yes, I've only two colors on the first image. It's strange that with HQX resizing built others colors... Anyway
So if I find a better resizing method (without create others values), the clut option will replace 0 value with #FFFFFF, 1 value with #6EA1FF, ...?
Thank you for your code ;) It's better indeed :)

Re: How use -clut option

Posted: 2014-04-05T11:03:03-07:00
by guillaumech
Other question! ;)

I don't understand why clut option desn't work...
Even if I've an image with severals color like that :
Image

Clut will transform each color between 0 and my first value on my gradient with the first color #FFFFFF (if I've just the 1 value on my image) no?
Thank you very much!

Re: How use -clut option

Posted: 2014-04-05T13:46:57-07:00
by snibgo
I think you misunderstand clut.

Where you have a pixel with red channel at minimum, it will lookup the first pixel in the clut file, and use the red value from there.

Where red is at maximum (eg 65535), it will lookup the last pixel in the clut file, and use the red value from there.

For intermediate values, it uses intermediate pixels from the clut.

The same applies to the green and blue channels.

Try:

Code: Select all

convert 03a.png -auto-level g2.png -clut out.png

Re: How use -clut option

Posted: 2014-04-05T15:22:16-07:00
by fmw42
I believe that you can add -interpolate nearestneighbor before -clut to ensure that no new colors are created.

Re: How use -clut option

Posted: 2014-04-05T16:09:36-07:00
by guillaumech
Thx Snibgo

Yes, I just understand how clut was made!
So, my problem is the gradient color on my image. I thought that clut will be able to "transform" several values in one. it's difficult to explain
There is my main image
http://nauton.espace-meteo.com/main.png

I would like to colorize this image. But, I have to resize it, so I've many solutions

Code: Select all

convert main.png -filter Lanczos2Sharp -resize 2080x1430 g2.png -interpolate Integer -clut outWelch.png;
http://nauton.espace-meteo.com/outWelch.png
But I've two blue colors... It's a gradient between white and real blue values. (on my main image I've only 2 colors => dark and one type of gray)

Code: Select all

scalex main.png -k 4 main.png;scalex main.png -k 4 main.png;convert main.png g2.png -interpolate Integer -clut outWelchScalex.png;
http://nauton.espace-meteo.com/outWelchScalex.png
But there are strange shape... Like a strange triangle :( The scalex algorithm it's good because it not built gradient values, but the result isn't very good

Code: Select all

hqx -s 4 main.png main.png;hqx -s 4 main.png main.png;convert main.png g2.png -interpolate Integer -clut outWelchHQX.png;
http://nauton.espace-meteo.com/outWelchHQX.png
Same problem than Lanczos2Sharp resize, I obtain a gradient.

I thought that the issue of my problem was clut option, but if you say no, maybe is resizing?
I'd just obtain an image which each value have one color. If my clut gradient is red to blue, and if I've an image with middle value, I just would like a color between blue and red, not blue+blue... from to real color
Maybe it's too difficult :(

Thx snibgo and fmw42 :)

Re: How use -clut option

Posted: 2014-04-05T16:25:25-07:00
by fmw42
If you have to resize the image, then either use either-scale before or after clut rather than -resize, since the latter will interpolate new values. That is if you do not want new colors. If you do want new shades, then -resize is fine. Or use -resize and then threshold since you say you only have to grayshades.

If you only have two colors, then the other way would be to autolevel so one color is black and the other color is white. Then just use -fill newcolor -opaque white (or -opaque black depending upon which one you want to color).

Perhaps I misunderstand what you are trying to do.


Code: Select all

convert main.png -scale 500% -auto-level -fill red -opaque black main_black2red.png
convert main.png -scale 500% -auto-level -fill red -opaque white main_white2red.png

Re: How use -clut option

Posted: 2014-04-05T16:29:23-07:00
by snibgo
First problem: main.png has a large canvas. I assume you want to ignore that, so I'll "+repage".

Next problem: there are many ways of resizing. If you want to preserve colours, without creating new ones, "-scale" is good. Windows BAT syntax:

Code: Select all

convert main.png +repage -scale "2080x1430^!" m.png
(For Unix, you probably don't need the ^ character.)

Does that help?

(EDIT: cross-posted with fmw, as often happens.)

Re: How use -clut option

Posted: 2014-04-06T03:49:05-07:00
by guillaumech
Thank you snibgo & fmw42, but the result with -scale isn't good for me :(

Indeed, scale don't create other values of color but built an image with boxes of colors, not curves.
I mean, this is my full problem. I try to build a weather map. So, I've one value (for exemple values of temperature) each kilometer. So, I built an gray image with the first pixel for the first value, second for the second value...
After, I would like resize this map to obtain a true map. Many solutions for resize : create others colors values and obtain smooth curves colors, not create others colors and obtain boxes area...

With scalex algorithm, the curve are smooth, but it create artefacts :(
Is this one solution?
I just see this method:

Code: Select all

montage dpat_map.gif   -geometry +5+0 -tile x1  -background none  \
          dpat_map_images.gif
  convert -seed 100 \
          -size 200x200 plasma:'gray(50%)-gray(50%)' -blur 0x15 \
          -channel G -auto-level +channel -set colorspace sRGB \
          dpat_map.gif -virtual-pixel tile  -fx 'u[floor(5.999*u.g)+1]' \
          map.gif
=> http://www.imagemagick.org/Usage/quantize/#threshold

Maybe use a fx function? To get severals colors on my gray image with smooth curves.
Additional, each values are important... If resizing lose value because there are too many others colors around it, it's a bad solution :(
Waouu, so complicated. Sorry for all questions :(
Thank you very much!

Re: How use -clut option

Posted: 2014-04-06T05:39:17-07:00
by snibgo
You might try some of the filters for resize, eg spline or triangle. Eg (Windows BAT syntax):

Code: Select all

convert main.png +repage -filter spline -resize "2080x1430^!" m.png
Type "convert -list filter" for alternatives.

Re: How use -clut option

Posted: 2014-04-06T06:23:45-07:00
by guillaumech
I tried all filters yes.
Spline create others colors :
http://nauton.espace-meteo.com/spline.png

Spline map have smooth borders yes, but now, I'd like colorize this map with only two colors, because on my main image, there are only two colors... (for this example, in true life it's false ;))
If I've just two values (1 : sun, 3 : rain), I can't invent others colors on the final map. I have to get just two values at the end.
To complete, with gradient : 1 : sun, 2 : cloudiness, 3 : rain, with gradient color 1 : black, 2 : red, 3 : white
If I've an image with just 1 and 3 value (sun and rain), the final map must to have color 1 and 3, not 2... with smooth curves borders
Maybe it's not possible...

Thank you snibgo

Edit:

I think the best way is this method

Code: Select all

montage dpat_map.gif   -geometry +5+0 -tile x1  -background none  \
          dpat_map_images.gif
  convert -seed 100 \
          -size 200x200 plasma:'gray(50%)-gray(50%)' -blur 0x15 \
          -channel G -auto-level +channel -set colorspace sRGB \
          dpat_map.gif -virtual-pixel tile  -fx 'u[floor(5.999*u.g)+1]' \
          map.gif
Because with Spline map (smooth borders), it take several values (with 5.999*u.g). I don't know if it's the best way :(

Re: How use -clut option

Posted: 2014-04-06T07:45:51-07:00
by snibgo
guillaumech wrote:I'd like colorize this map with only two colors, because on my main image, there are only two colors
"-remap" can do this, eg:

Code: Select all

convert main.png +repage -write mpr:XX -filter spline -resize "2080x1430^!" +dither -remap mpr:XX m.png
Remove "+dither", if you want.

Re: How use -clut option

Posted: 2014-04-06T11:45:10-07:00
by fmw42
If you use -resize, it will create new grayscale values (and thus after coloring, new colos). The only way I know to keep only two is the threshold after resizing, but that will make the edges aliased (stair-steps).

Re: How use -clut option

Posted: 2014-04-06T13:10:09-07:00
by guillaumech
This way is interesting, but if my main map is : Image

With

Code: Select all

convert main.png +repage -write mpr:XX -filter spline -resize 2080x1430 +dither -remap mpr:XX m.png
I obtain this map
Image

It's better but I've wrong values around the right bottom area (with 68,68,68 values). On my main map there are juste area in this place with 68,68,68 values, none colors around with 34,34,34 values.
In my case, it's a problem, because if 68,68,68 values is the sun and 34,34,34 is the rain, there not always rain around the sun... Perhaps that my explications are bad :(
How obtain my main map with 2080x1430 size, with just colors on the main map, and smooth edges?

I can have many values on my main map. But each values must to be on the final map, and just this values.