Page 1 of 1

Color reduction and using palettes

Posted: 2018-05-28T14:41:03-07:00
by guy lateur
Hi all, new user here, so please bear with me.. ;)

I'm trying to use IM in my 'modern day Amiga workflow'. I'll usually be starting from a high resolution 8bit/channel image that has, say, been rendered in Blender. I'll need to convert this image to a low resolution paletted/color-indexed version of it. I'll probably be able to handle the resolution change, but let's focus on color reduction ('palettization') for now.

The (original) Amiga uses color-indexed images with a maximum of 32 colors. Of course, it handles any number of colors that is a power of 2, so 2 (b/w), 4, 8, 16 or 32. The bit depth per channel is 4, so a color is represented by 3 'nibbles', eg $000 represents black, $888 grey and $fff white. There's a total of 16³ = 4096 colors to choose from.

Eg, I'm doing a lot of these:

Code: Select all

convert IN.jpg -colors 16 -depth 4 OUT.png
The result looks very good, but I have a couple of questions:

1) If I look at the properties of the result (magick identify -verbose OUT.png), it will usually have the expected number of colors (eg, 16), but it tends to have 1 more color (so 17 in total) in the 'Colormap entries', usually white. Is this intended? Can it be avoided? Because I'll obviously also need easy access to (read: I'd like to copy & paste) the generated palette.

2) Can I specify the colors in the resulting palette myself? As I said, the conversion usually looks good and everything, but it would definitely be handy if I could specify some (or all) of the colors myself. Is this possible at all?

TIA for any thoughts/tips on this!

Re: Color reduction and using palettes

Posted: 2018-05-28T15:02:16-07:00
by snibgo
What version IM? On what platform?

1. Can you supply an example of an IM process that makes an output with 16 colours but 17 or more colormap entries?

2. To specify colours yourself, use "-remap", supplying an image that has only the colours you want. The result may not use all the colours in that image, but it won't use any other colours.

There is no simple method for supplying some of the desired colours, and I don't know exactly what you mean by that. You can use "-remap" to specify the only colours that may be used, and you could then find which pixels have changed by more than some threshold, then reduce just those colours by some other method.

Re: Color reduction and using palettes

Posted: 2018-05-28T15:26:47-07:00
by guy lateur
ImageMagick 7.0.7-35 Q16 x64 2018-05-21, on win10pro.

1)

Code: Select all

convert IN.jpg -colors 16 -depth 8 OUT.png
The input is a picture of 2 people; nothing special.

2) Ok, I'll look into the remap option, thanks. And about not using all colors: I've had conversions to 16 color palettes (4 bits/channel) that ended up using only 14 colors. But I guess this is possible, ie. that any other color in this coarse color space would be further away from the colors already in the palette.

The specification of some of the desired colors would be handy if you would like to share certain colors between various images. You only have 1 'run time' palette on the Amiga, so it can be desirable to display 2 images without having to change the palette. So you're basically trying to optimise the palette for 2 (or more) images at once. But as you say, if I understand this -remap thing correctly, I should be able to do this manually.

Thanks for replying, btw, much appreciated!

Re: Color reduction and using palettes

Posted: 2018-05-28T15:53:56-07:00
by snibgo
For (1), sorry, I should have said can you supply a repreducible example, ie the code and a link to the input image, something we can test ourselves in different versions of IM to see if we get the same result?
guy lateur wrote:So you're basically trying to optimise the palette for 2 (or more) images at once.
You can do that, for example by appending 2 (or more) input images together, then "-colors 16", then "-unique-colours" to get an image with just those 16 (or fewer) colours. If the images are not the same size, appending will introduce a background colour that messes the reduction calculation, so you can avoid that by splitting all the inputs into rows, and appending those sideways.

Code: Select all

magick ^
  in1.png in2.png in3.png ^
  -crop x1 +append +repage ^
  -colors 16 -unique-colors ^
  map.png
(Windows syntax. For bash, replace line-end "^" with "\".)

map.png contains up to 16 pixels, and can be used to individually "-remap" the inputs. BUT there is no guarantee that each input will use all 16 colours, or the same subset of the colours.

Re: Color reduction and using palettes

Posted: 2018-05-28T16:32:14-07:00
by guy lateur
About 1): here's the input: http://users.telenet.be/guy.lateur/IM/IN-colormap17.jpg
Here's the output I get: http://users.telenet.be/guy.lateur/IM/OUT-16-8.png
As mentioned, this is the command that generates that:

Code: Select all

magick convert IN-colormap17.jpg -colors 16 -depth 8 OUT-16-8.png
About 2): excellent idea! This will surely help a lot towards finding those 'optimal' palettes, so I'll definitely be looking into this (+ remap).

Re: Color reduction and using palettes

Posted: 2018-05-28T18:12:53-07:00
by fmw42
I suggest you add +dither or -dither none before -colors to avoid getting dithered results.

Re: Color reduction and using palettes

Posted: 2018-05-29T03:44:20-07:00
by guy lateur
fmw42 wrote: 2018-05-28T18:12:53-07:00 I suggest you add +dither or -dither none before -colors to avoid getting dithered results.
Thanks for the tip, I'm aware of that switch. I don't think it'll be a question of always having dither on or off. Sometimes (like with a picture) dithering can be beneficial to the overall quality. Anyway, turning dither on or off has no effect on there being 17 colors in the colormap, instead of the expected 16 that are actually in the image, as explained earlier.

Re: Color reduction and using palettes

Posted: 2018-05-29T03:51:21-07:00
by snibgo
The extra colormap entry seems to be the background colour (which defaults to white, but can be changed with "-background"). To avoid that being added to the colormap, excluding the PNG background chunk seems to work:

Code: Select all

f:\web\im>%IMG7%magick convert IN-colormap17.jpg -colors 16 -background red -define png:exclude-chunk=bKGD x.png

f:\web\im>%IMG7%magick identify -verbose x.png |grep Colormap
  Colormap entries: 16
  Colormap:
Tested with IM v7.0.7-28.

Re: Color reduction and using palettes

Posted: 2018-05-29T04:03:13-07:00
by guy lateur
snibgo wrote: 2018-05-29T03:51:21-07:00 The extra colormap entry seems to be the background colour (which defaults to white, but can be changed with "-background"). To avoid that being added to the colormap, excluding the PNG background chunk seems to work:

Code: Select all

f:\web\im>%IMG7%magick convert IN-colormap17.jpg -colors 16 -background red -define png:exclude-chunk=bKGD x.png

f:\web\im>%IMG7%magick identify -verbose x.png |grep Colormap
  Colormap entries: 16
  Colormap:
Tested with IM v7.0.7-28.
Yep, that does the trick indeed, thank you very much! 8)