Color Replacement... question on -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
dognose
Posts: 265
Joined: 2005-03-08T22:16:37-07:00

Color Replacement... question on -clut

Post by dognose »

I have a grayscale image that's for levels of gray (-colors 4)

I want to replace each of those colors with 4 colors from a lookup table -clut

The problem I'm having is the some of the gray colors are close to each other and the output image only ends up with 2 colors... ie. it won't use all 4 colors.

convert input.gif \( clut.gif \) -interpolate Integer -clut output.gif

I've tried normalizing or equalizing. Is there an easy way to replace the 4 colors or balance the image?

I don't want to have to parse the colors of the image then individually replace.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Color Replacement... question on -clut

Post by fmw42 »

dognose wrote:I have a grayscale image that's for levels of gray (-colors 4)

I want to replace each of those colors with 4 colors from a lookup table -clut

The problem I'm having is the some of the gray colors are close to each other and the output image only ends up with 2 colors... ie. it won't use all 4 colors.

convert input.gif \( clut.gif \) -interpolate Integer -clut output.gif

I've tried normalizing or equalizing. Is there an easy way to replace the 4 colors or balance the image?

I don't want to have to parse the colors of the image then individually replace.

We would need to see your image.gif and clut.gif to see what you are trying to do and why you are having trouble.

But you could do:

convert image.gif \
-fill color1 -opaque gray1 \
-fill color2 -opaque gray2 \
-fill color3 -opaque gray3 \
-fill color4 -opaque gray4 \
output.gif
dognose
Posts: 265
Joined: 2005-03-08T22:16:37-07:00

Re: Color Replacement... question on -clut

Post by dognose »

I was hoping you wouldn't say that. haha. I thought I was just using clut wrong...


Since I have to do this automatically, and for a varied number of colors, so, it's sorta hard to write it directly..

convert image.gif -format "%c" histogram:info:

parse that

sort the colors (light to dark)

repeat for the lookup table

then remap each color individually.

..

it seems like there should be an easier way. Is there another way replace the color table?

Here is an example with 5 colors of gray

Image

convert xc:red xc:green xc:blue xc:yellow xc:white +append clut.gif
convert in.gif \( clut.gif \) -interpolate integer -clut out.gif

Image

note how the output image only has 4 colors and not 5. it's missing the blue.

convert --version
Version: ImageMagick 6.3.8 01/31/08 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2008 ImageMagick Studio LLC
Last edited by dognose on 2009-01-19T19:33:26-07:00, edited 2 times in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Color Replacement... question on -clut

Post by fmw42 »

Perhaps you are just not using -clut correctly. Show us an example set of data to play with.
dognose
Posts: 265
Joined: 2005-03-08T22:16:37-07:00

Re: Color Replacement... question on -clut

Post by dognose »

sorry, I just added that to my last post after you already replied.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Color Replacement... question on -clut

Post by fmw42 »

I think it has to do with -interpolate integer (possibly not working correctly or as we expect??)

This seems to work for me to get five colors, but not exactly the ones you want.

identify -verbose grayscale545.gif
Histogram:
84846: ( 2, 2, 2) #020202 rgb(2,2,2)
42700: (173,173,173) #ADADAD grey68
30149: (215,215,215) #D7D7D7 rgb(215,215,215)
17307: ( 94, 94, 94) #5E5E5E grey37
12498: ( 44, 44, 44) #2C2C2C rgb(44,44,44)

convert xc:red xc:green xc:blue xc:yellow xc:white +append clut.gif
convert grayscale545.gif clut.gif -clut grayscale545_clut1.gif


Image

identify -verbose grayscale545_clut1.gif

Histogram:
84846: (247, 4, 0) #F70400 rgb(247,4,0)
42700: (182,182, 73) #B6B649 rgb(182,182,73)
30149: (255,255, 95) #FFFF5F rgb(255,255,95)
17307: ( 0, 67,121) #004379 rgb(0,67,121)
12498: ( 79, 88, 0) #4F5800 rgb(79,88,0)


whereas

convert grayscale545.gif clut.gif -interpolate integer -clut grayscale545_clut2.gif

Image

does have only 4 of your 5 colors.

identify -verbose grayscale545_clut2.gif

97344: (255, 0, 0) #FF0000 red
42700: (255,255, 0) #FFFF00 yellow
30149: (255,255,255) #FFFFFF white
17307: ( 0,128, 0) #008000 green


It does seem odd that the middle color in your clut.gif is the one that is skipped.

Hopefully Anthony can shed some light on this.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Color Replacement... question on -clut

Post by anthony »

The color lookup form the CLUT mapping image is linear, according to the
grey color. However your gray colors are NOT linear!

That is your two darkest grays map to the same color!

To remap you image using a CLUT, you will need to adjust your greys to be
a constant spacing, as you CLUT colors are constant spaced!!!!


As you can't really control the grey levels exactly may I offer an alternative solution. Output the image to palette image format and replace the palette!

For example save the image to a 'XPM' text format...

Code: Select all

convert grayscale545.gif -type TrueColor  grayscale545.xpm
The -type in the above tell IM to 'forget' about the existing palette it remembers from the GIF image, so the XPM image has the smallest possible palette and not a power of 2 sized palette that GIF would use.
Now looking at the top of this text file image...
/* XPM */
static char *xpm__[] = {
/* columns rows colors chars-per-pixel */
"500 375 5 1",
" c #020202",
". c #2C2C2C",
"X c gray37",
"o c gray68",
"O c #D7D7D7",
/* pixels */
So you can now replace the string following the 'c' with what ever colors you want, and convert the image to whatever format you normally want.


It is a pain that IM does not provide a direct palette replacement, but this palette ordering is also not very well defined when IM is forced to re-create the color palette (which I do in the above)
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
dognose
Posts: 265
Joined: 2005-03-08T22:16:37-07:00

Re: Color Replacement... question on -clut

Post by dognose »

Wow, interesting, editing the xpm file. It certainly works.... I guess I may use that.

I'm surprised there isn't a way to swap out the color palette via the command though, or somehow make the colors linear.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Color Replacement... question on -clut

Post by anthony »

dognose wrote:I'm surprised there isn't a way to swap out the color palette via the command though, or somehow make the colors linear.

If you want 'equally spaced' colors from the original image (so it matched you equally spaced CLUT) than you can use posterize on a greyscale image.

But I have a feeling that that your initial set of grays are not ment to be equally spaced, but the 'best set of greys' to represent the image.

As you are swapping them for a 'Angry Fruit Salad' color scheme I also think you are doing 'Andy Warhol' type image modifications.

The Alternative to is to take your histogram of colors, extract the gray values, sort them and generate the appropriate -fill -opaque color changes operators. May be faster than actually doing multiple converts, especially on larger images.


WARNING: I can no guarantee the colors in the XPM are in sequence, though they probably are.

that should be quite scriptable as well.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply