Page 1 of 1

Converting a gradient RGB to indexed file

Posted: 2018-05-30T08:21:23-07:00
by tresorjoe
Hello,

I encounter an issue when trying to index a gradient file in RGB 8bit with following command line:

Code: Select all

convert -verbose Gradient.tif -dither none -colors 21 Gradient.png
I get the verbose information that the resulting png file only contains 16 colors, not 21 as specified. If I use a larger number such as

Code: Select all

convert -verbose Gradient.tif -dither none -colors 60 Gradient.png
it shows that ImageMagick in deed is able to generate 21 colors (well, still not the 60 that I specified specifically).

Any idea what's happening?

Here's the RGB 8bit file I'm using:
https://www.dropbox.com/s/150p822mfsgm ... t.tif?dl=0

Thanks!

Re: Converting a gradient RGB to indexed file

Posted: 2018-05-30T08:57:49-07:00
by snibgo
The colour reduction algorithm used by "-colors N" guarantees the result will have no more than N colours, but it does NOT guarantee any particular minimum.

That's simply the way the algorithm works.

The developers might be interested in code that implements other colour reduction methods.

You can implement a loop that tries different values of N in "-colors N" to find the result that has the largest number of unique colours that are not more than your required goal. This wouldn't be fast. In your example, "-colors 28" is the smallest N that delivers exactly 21 unique colours. (In fact, I would find this useful, so I might implement it as a process module.)

Re: Converting a gradient RGB to indexed file

Posted: 2018-05-30T09:10:43-07:00
by tresorjoe
Thanks, snibgo
Didn't know that
"-colors N" guarantees the result will have no more than N colours, but it does NOT guarantee any particular minimum.
Your loop process sounds like a good idea.

Re: Converting a gradient RGB to indexed file

Posted: 2018-06-04T18:12:01-07:00
by snibgo
[I've removed spam from the thread.]