Converting a gradient RGB to indexed file

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
tresorjoe
Posts: 13
Joined: 2018-04-12T02:19:12-07:00
Authentication code: 1152

Converting a gradient RGB to indexed file

Post 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!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Converting a gradient RGB to indexed file

Post 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.)
snibgo's IM pages: im.snibgo.com
tresorjoe
Posts: 13
Joined: 2018-04-12T02:19:12-07:00
Authentication code: 1152

Re: Converting a gradient RGB to indexed file

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Converting a gradient RGB to indexed file

Post by snibgo »

[I've removed spam from the thread.]
snibgo's IM pages: im.snibgo.com
Post Reply