Color reduction doesn't work well with alpha PNGs

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
jhuckaby
Posts: 15
Joined: 2013-03-29T09:40:03-07:00
Authentication code: 6789

Color reduction doesn't work well with alpha PNGs

Post by jhuckaby »

Consider this sample 32-bit alpha PNG image:
Image

When converted to 256 colors, ImageMagick 6.3 does a fantastic job:

Code: Select all

convert alpha-source.png -dither None -colors 256 alpha-256-im63.png
ImageMagick 6.3 Output: Image

However, the image looks considerably worse when I use ImageMagick 6.8:

Code: Select all

convert alpha-source.png -dither None -colors 256 alpha-256-im68.png
ImageMagick 6.8 Output: Image

As you can see, the IM 6.8 image has strange artifacts and what appear to be palette errors.

Here is another example from ImageMagick's own Quantize Page (http://www.imagemagick.org/Usage/quantize/):

Source Image: Image

Code: Select all

convert alpha_gradient.png -dither None -colors 256 alpha_gradient-256-im63.png
ImageMagick 6.3 Output: Image

Code: Select all

convert alpha_gradient.png -dither None -colors 256 alpha_gradient-256-im68.png
ImageMagick 6.8 Output: Image

As you can see, the ImageMagick 6.3 version looks considerably better.

Did something change in the ImageMagick color reduction algorithms?

I am using:

Version: ImageMagick 6.8.4-6 2013-04-04 Q8 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2013 ImageMagick Studio LLC
Features: DPC
Delegates: bzlib freetype jng jpeg lcms png tiff xml zlib
OS: Linux 2.6.32-220.el6.x86_64 #1 SMP Tue Dec 6 19:48:22 GMT 2011 x86_64 x86_64 x86_64 GNU/Linux (64-bit CentOS 6.2)

- Joe
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Color reduction doesn't work well with alpha PNGs

Post by snibgo »

Try ...

Code: Select all

"%IMG6846%convert" alpha-gradient.png  -quantize RGB -alpha off -dither None -colors 256 -alpha on a8.png
... which gives a similar result to that old IM version but (I would argue) even more fantastic.
snibgo's IM pages: im.snibgo.com
jhuckaby
Posts: 15
Joined: 2013-03-29T09:40:03-07:00
Authentication code: 6789

Re: Color reduction doesn't work well with alpha PNGs

Post by jhuckaby »

Hey snibgo,

Thanks! I did see that command-line trick in the ImageMagick Quantize docs, however, it produces a much larger file size:

Original 32-bit PNG: 10.6K
Converted 8-bit PNG: 5.4K
With the -alpha off -alpha on thing: 7.8K

I was hoping to keep the size down, but the quality up, like ImageMagick 6.3 does. Also, there is no way to do this in PerlMagick (no "alpha" option in the Quantize filter). Sigh.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Color reduction doesn't work well with alpha PNGs

Post by snibgo »

Sorry, I don't speak Perl.

I forgot to mention: I use Q16 IM, which will give different results to your Q8.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Color reduction doesn't work well with alpha PNGs

Post by fmw42 »

jhuckaby wrote:Also, there is no way to do this in PerlMagick (no "alpha" option in the Quantize filter). Sigh.
I don't use Perlmagick either, but looking at the docs, see http://www.imagemagick.org/script/perl- ... -attribute

"alpha {On, Off, Opaque, Transparent, Copy, Extract, Set} control of and special operations involving the alpha/matte channel"
jhuckaby
Posts: 15
Joined: 2013-03-29T09:40:03-07:00
Authentication code: 6789

Re: Color reduction doesn't work well with alpha PNGs

Post by jhuckaby »

Ah, thank you fmw42, I did not know that could be used with the 'Quantize' filter. I'll play around with it.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Color reduction doesn't work well with alpha PNGs

Post by fmw42 »

It is a setting that you use before and/or after some processing to enable or disable the alpha channel. It is not specific to any one method.
Post Reply