png8 and index/alpha 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
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

png8 and index/alpha transparency

Post by Bonzo »

I have read Anthony's pages about png8 and transparency and understand that if the image is modified later the transparency will be lost. I asumed that if I just saved the image at the end of manipulation all would be OK but its not.

I have read an artical ( http://blogs.sitepoint.com/2007/09/18/p ... ar-winner/ ) that says I want to use alpha transparency not indexed transparency - is there a way to set this?

This is the command I used:

Code: Select all

exec("convert shed.jpg -resize 200x200 -bordercolor white -border 10x10 temp.png");
exec("convert \( temp.png drawing_pin.png -geometry +180+10 -composite \) -background none -rotate \"-15\" png8:shed_png8.png");
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: png8 and index/alpha transparency

Post by glennrp »

"alpha transparency" is PNG or PNG32.

GIF-style transparency (one color is defined to be transparent)
is PNG8 or PNG24 with the tRNS chunk that names a single
transparent color index for PNG8 or a single transparent color for PNG24.

ImageMagick will write an indexed PNG with multiple alpha values
if it can (i.e., when there are 256 or fewer R,G,B,A entries in the
colormap), but it's not a PNG8 if more than one color is
transparent or if any color is semitransparent. Write a PNG32
to prevent that from happening if you don't want it to.

You can exert more detailed control over what the PNG
encoder writes using the "-define PNG:keyword=value"
to set the PNG colortype, bitdepth, and what ancillary chunks
it includes or excludes.

Glenn
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: png8 and index/alpha transparency

Post by anthony »

What is happening is that the coder for the normal "PNG" format is being modified to that it can save to a PNG indexed image with multiple transparency when not too many colors are present in the image being saved.

However over the last few releases various bugs have appears that erroneously turns alpha on or off in the saved index PNG image. This is being handled by Glennrp, who is the programmer behind the PNG coder.

As per Glenn's response PNG8 is still a 'GIF' like PNG image. And PNG24 and PNG32 can still be used to override the indexing PNG style that the normal PNG format may attempt to use.

At least this is my understanding of the situation. Glenn can you clarify?

Glenn -- Is there a new 'format' to specifically request indexed PNG? (which like PNG8 may require color reduction)
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: png8 and index/alpha transparency

Post by Bonzo »

Thanks for the info; what I want is a png image that renders with variable transparency in new browsers and resorts to the gif type transparency in older browser.

I have found a program that will do what I want - pngquant - the quality is not great in IE6 but it works.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: png8 and index/alpha transparency

Post by anthony »

pngquant is a color reducer (256 color limit) but as a specialized PNG program it creates semi-transparent indexed images correctly. This is what the PNG coder is working on providing.

However you need to remember that IM is a general mutli-format image processor, and can not always specialise in one specific format. It can be beaten by more specialised programs. Things are improving though.

One of my own questions was that now that the coder can do it, how to force IM to generate a indexed PNG with semi-transparency. The answer may solve your problem, especially as things improve.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: png8 and index/alpha transparency

Post by glennrp »

I have revised the PNG encoder to do color reduction when writing a PNG8 instead of
simply rejecting the image when there are more than 256 colors. It's just a simple
truncation algorithm that reduces the image to a 3-3-2 palette. If you want
better, you must reduce the number of colors to 256 (or 255, if the background
color isn't in the palette) prior to calling the PNG decoder.

Similarly, if the image has translucent pixels they are converted to binary
by a simple 50% threshold.

These features are checked into the SVN head (r3729) and should be in IM-6.6.7-10.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: png8 and index/alpha transparency

Post by anthony »

Instead of a 3-3-2 'posterize' palette, could you not just do a -colors 256
By default that generates both transparent and semi-transparnt colors.

EG for PNG8 just do -colors 256
with no other settings or alpha thresholding.

GIF however does some special handling for color reduction as it only wants a single boolean full-transparency color.
Specifically it alpha thresholds, and uses a -quantize transparency setting before color reduction to 255 colors when a transparent color is present after the threshold (256 colors if no transprency).

EG the psuedo code for GIF should be following...

Code: Select all

    -channel A -threshold 50%
    if (fully-)transparent pixels are present it then...
      -quantize transparent -colors 255
    otherwise if no transparent pixels present...
      -colors 256
Of course -colors should short-circuit if less than the requested number of colors is present.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply