How to reduce number of colors the right way?

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
pbeitbeor
Posts: 7
Joined: 2016-09-03T10:37:45-07:00
Authentication code: 1151

How to reduce number of colors the right way?

Post by pbeitbeor » 2017-04-04T09:17:43-07:00

I want to reduce the number of colors of a jpg image to 16, with the following command:

convert -depth -colors 16 image.jpg image-16.jpg

When I count the colors in the image it's about 24000.

What is the correct convert syntax to get a color count of 16?

Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to reduce number of colors the right way?

Post by Bonzo » 2017-04-04T09:27:17-07:00

-depth should have a value or not be there which may be your problem.

User avatar
fmw42
Posts: 25764
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to reduce number of colors the right way?

Post by fmw42 » 2017-04-04T09:37:02-07:00

Proper IM 6 syntax reads the input image first, then the settings, then any operators, then the output. So your command should be something like

Code: Select all

convert image.jpg +dither -colors 16 image-16.jpg
However, jpg is compressed and thus the compression will likely change your colors and you will probably get more colors than you want. If you save to GIF, you can reduce the colors without producing more colors due to the jpg compression.

Code: Select all

convert image.jpg +dither -colors 16 -depth 4 image-16.jpg
+dither prevents -colors from dithering the colors

pbeitbeor
Posts: 7
Joined: 2016-09-03T10:37:45-07:00
Authentication code: 1151

Re: How to reduce number of colors the right way?

Post by pbeitbeor » 2017-04-04T12:54:40-07:00

Thanks to both of you.

I gave it a little twist. I converted the image to png (not gif) and used the last command with it.
That did it for me.

User avatar
fmw42
Posts: 25764
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to reduce number of colors the right way?

Post by fmw42 » 2017-04-04T12:57:52-07:00

Palette PNG is similar to GIF. So that is fine

Post Reply