Quantise png preserving semi transparency

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
spiderplant0
Posts: 16
Joined: 2012-11-15T06:43:37-07:00
Authentication code: 6789

Quantise png preserving semi transparency

Post by spiderplant0 »

Hi, I have some png files, they dont have many colors in them so they are candidates for file size reduction by converting to palette PNGs. I.e. can ImageMagick read in a PNG, quantise it by analysing it to work out the best set of 16 colors (RGB and alpha) to represent it and then saving it as a 4-bit PNG. On the Windows command line. Can ImageMagick do this conversion? If not, can it convert to 256 colors (8 bit). Note there are semi-transparent pixels involved.

In the manual it says it that it doesn't deal with semi-transparent pixels, not sure if this is just the default.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Quantise png preserving semi transparency

Post by glennrp »

ImageMagick-6.8.0-5 will write a paletted PNG by default if the input image has 256 or fewer RGBA colors (including
the background color), even if some of them are semitransparent. Just run "convert input.png output.png". To reduce
the color count to 16 use "convert input.png -colors 16 output.png". To get the bit-depth down to 4 you might need to
use "-colors 15" instead of "-colors 16" to leave room for the background color in the palette.

What is the manual reference that you mentioned?
spiderplant0
Posts: 16
Joined: 2012-11-15T06:43:37-07:00
Authentication code: 6789

Re: Quantise png preserving semi transparency

Post by spiderplant0 »

Thanks glenrp, that worked perfectly.

FYI, the bit in the manual I was referring to ...
http://www.imagemagick.org/Usage/formats/#png_formats
it says: "PNG8 was defined by PhotoShop, not the PNG group. And while it can handle multiple semi-transparent colors, as well as a fully-transparent color, IM assumes that it doesn't"
And when I tried the suggested command: "convert {input_image} -type Palette indexed.png",
it indeed removed all partially transparent pixels

Maybe I didnt read enough, but at this point I assumed ImageMagick could not handle partial transparency and so nearly gave up on it.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Quantise png preserving semi transparency

Post by fmw42 »

And when I tried the suggested command: "convert {input_image} -type Palette indexed.png",
to retain binary transparency, try

convert {input_image} -type Palettematte indexed.png

or

convert {input_image} PNG8:indexed.png

or

convert {input_image} -type Palettematte PNG8:indexed.png


If those don't work, the Glenn would likely have the commands to use from http://www.imagemagick.org/Usage/formats/#png_write
Post Reply