Page 1 of 2
PNG8 broken after -transparent
Posted: 2010-06-22T11:44:01-07:00
by UICodes
Hi, after I issue the simple command:
mogrify -transparent "#FB00FF" img.png
the png "breaks" (not visible in all my platforms).
Fix: I subsequently run pngquant on the same png with
pngquant -verbose 256 img.png.
Does anyone know the IM commands to reproduce the pngquant steps and fix the image? It's not feasible for me to run both IM and pngquant on all images.
I noticed that after running pngquant, the image contains a tRNS chunk.
Thanks in advance.
Image is attached:

Re: PNG8 broken after -transparent
Posted: 2010-06-22T12:34:37-07:00
by fmw42
mogrify -transparent "#FB00FF" img.png
On a single image, you use convert not mogrify. Mogrify is for a folder of images.
This works for me
convert img.png -transparent "#FB00FF" img2.png
Re: PNG8 broken after -transparent
Posted: 2010-06-22T12:48:19-07:00
by UICodes
fmw42 wrote:On a single image, you use convert not mogrify. Mogrify is for a folder of images.
This works for me
convert img.png -transparent "#FB00FF" img2.png
I read on the site that mogrify is used to convert an image and overwrite it at the same time.
Thanks for the reply but the issue seems to be deeper than selecting between convert/mogrify.
Both commands will render the image transparent but the output image does not contain the PLTE and tRNS chunks.
Are there IM commands to force the image to include PLTE and tRNS (similar to what pngquant does)?
Thanks in advance.
Re: PNG8 broken after -transparent
Posted: 2010-06-22T14:02:17-07:00
by Drarakel
On my system, your command works and nothing is lost.
IM will create a truecolor+alpha PNG (so you have full 4x8bit). Naturally, there can't be a PLTE and tRNS chunk in there. A PNG optimizing program will 'reduce' it to palette with a tRNS chunk, yes. But both files should be correctly readable.
I think that directly writing the file as palette PNG (even when it has only binary transparency) is not possible with the current IM versions - there are some problems with it.
Re: PNG8 broken after -transparent
Posted: 2010-06-22T15:07:09-07:00
by fmw42
I believe you can have a palette image with binary transparency as long as the underlying transparent color is not used any where else in the image. Although this topic was partially for a different objective, the user wanted that format. See my tests and glennrp's comments.
viewtopic.php?f=1&t=15584&start=15
Hope this is what you are getting at and not my misunderstanding.
Re: PNG8 broken after -transparent
Posted: 2010-06-22T16:21:37-07:00
by Drarakel
fmw42 wrote:I believe you can have a palette image with binary transparency as long as the underlying transparent color is not used any where else in the image.
It's true for older versions. (In some, even partial transparency could get written as palette PNG!) But in the current versions, I can't get it to work. (I'm in the process of typing a text because of this at the moment.)
But thanks for the link, Fred! Interesting thread.
Re: PNG8 broken after -transparent
Posted: 2010-06-22T16:51:37-07:00
by UICodes
I'm not sure but it is the best lead I've seen so far. Thanks everyone... I'll post back if I get positive results.
Re: PNG8 broken after -transparent
Posted: 2010-06-22T17:07:56-07:00
by fmw42
Drarakel wrote:
It's true for older versions. (In some, even partial transparency could get written as palette PNG!) But in the current versions, I can't get it to work. (I'm in the process of typing a text because of this at the moment.)
But thanks for the link, Fred! Interesting thread.
Version of what? IM? libpng?
It was not too long ago that I replied to that thread. Let me test again.
Re: PNG8 broken after -transparent
Posted: 2010-06-22T17:27:37-07:00
by UICodes
Hi, The info that Glenn posted is a good start (I want the color-type 2 image with the tRNS chunk).
I'm new to IM, can someone please clarify these steps with examples:
1. Convert the transparency to binary (all alpha being 0 or 255) by thresholding or dithering.
2. Convert any magenta pixels to something nearly magenta (e.g., change the blue component from 255 to 254 in any magenta pixels).
3. Convert all transparent pixels to magenta.
4. Write the PNG.
I'm hoping that once my png's conform to the above, issuing convert -transparent will equate to step 4 and I'll have my working png.
Thanks in advance.
Re: PNG8 broken after -transparent
Posted: 2010-06-22T17:52:36-07:00
by fmw42
That is pretty much what I was doing before. This works. I make white into binary transparent (black or white). Then I extract the alpha channel. Then I flatten the image over green (which is not a color in my image). Then I put the alpha channel back.
convert logo: logo.png
convert \( logo.png -transparent white \) \
\( -clone 0 -alpha extract \) \
\( -clone 0 -background green -flatten \) \
-delete 0 +swap -alpha off -compose copy_opacity -composite \
logo_palette_transp_green.png
identify -verbose logo_palette_transp_green.png
Image: logo_palette_transp_green.png
Format: PNG (Portable Network Graphics)
Class: DirectClass
Geometry: 640x480+0+0
Resolution: 72x72
Print size: 8.88889x6.66667
Units: Undefined
Type: PaletteMatte
Endianess: Undefined
Colorspace: RGB
Depth: 8-bit
Channel depth:
red: 8-bit
green: 8-bit
blue: 8-bit
alpha: 1-bit
Channel statistics:
Red:
min: 0 (0)
max: 255 (1)
mean: 18.0637 (0.070838)
standard deviation: 56.684 (0.22229)
kurtosis: 9.5292
skewness: 3.29833
Green:
min: 6 (0.0235294)
max: 255 (1)
mean: 124.322 (0.487535)
standard deviation: 29.3121 (0.114949)
kurtosis: 6.99327
skewness: -0.138574
Blue:
min: 0 (0)
max: 245 (0.960784)
mean: 21.9873 (0.0862246)
standard deviation: 54.4317 (0.213458)
kurtosis: 4.71114
skewness: 2.43479
Alpha:
min: 0 (0)
max: 255 (1)
mean: 44.6997 (0.175293)
standard deviation: 96.9555 (0.380217)
kurtosis: 0.917287
skewness: -1.70801
# now turn off the alpha to see if background is green
convert logo_palette_transp_green.png -alpha off logo_palette_transp_green_aoff.png

Re: PNG8 broken after -transparent
Posted: 2010-06-22T18:10:55-07:00
by fmw42
1. Convert the transparency to binary (all alpha being 0 or 255) by thresholding or dithering.
2. Convert any magenta pixels to something nearly magenta (e.g., change the blue component from 255 to 254 in any magenta pixels).
3. Convert all transparent pixels to magenta.
4. Write the PNG.
I think what he is saying is:
1) modify your alpha channel to make sure it is binary (black and white only, no grays) so it is not partial transparency. You can do that with thresholding such as
convert image -channel a -threshold 50% +channel result1
2) If you want magenta to be the under transparency color, then you have to make sure there are no non-transparent magenta colors in your image. Thus you need to translate any of these to something close to magenta. Something like
convert result1 -alpha off -fill "rgb(254,0,254)" -opaque "rgb(255,0,255)" -alpha on result2
3 and 4) Then you want to make all the pixels under the transparent areas into magenta. Something like extract the alpha channel, flatten the image to magenta, then put back the alpha channel.
convert result2 \( -clone 0 -alpha extract \) \( -clone 0 -background magenta -flatten \) -delete 0 +swap -alpha off -compose copy_opacity -composite finalresult
Re: PNG8 broken after -transparent
Posted: 2010-06-22T20:34:36-07:00
by Drarakel
fmw42 wrote:Version of what? IM? libpng?
I'm generally using the standard IM Q16 binaries for Windows. I was using v6.6.2-5 for a lot of tests - could update to v6.6.2-7 today. The libpng version seems to be v1.2.43 here.(?)
Your images and your instructions are fine. But - as I said, it doesn't work that way in the current IM versions. Your 'logo_palette_transp_green.png' file is a full RGBA PNG - saved with 4x8 bit. One can't take the info from IM as hint that the file is indexed.
Re: PNG8 broken after -transparent
Posted: 2010-06-22T20:37:14-07:00
by fmw42
One can't take the info from IM as hint that the file is indexed.
Out of curiosity and my own education, what tool or method are you using to determine if it really is palette or not?
Fred
Re: PNG8 broken after -transparent
Posted: 2010-06-22T20:58:55-07:00
by Drarakel
At the moment, I'm mainly using TweakPNG. It's a nice little tool:
http://entropymine.com/jason/tweakpng/
Edit: Most of the tools that I'm using are only for Windows. I don't know what are the best tools for Unix or Mac.. As alternative, one could use a PNG optimizing tool like OptiPNG:
http://optipng.sourceforge.net/
This also shows the main properties of a PNG file.
Re: PNG8 broken after -transparent
Posted: 2010-06-23T10:15:16-07:00
by UICodes
Drarakel wrote:
Your 'logo_palette_transp_green.png' file is a full RGBA PNG - saved with 4x8 bit. One can't take the info from IM as hint that the file is indexed.
Thanks for the tips everyone. After running fmw42's command:
convert ( img.png -transparent #FB00FF ) ( -clone 0 -alpha extract ) ( -clone 0 -background #FB00FF -flatten ) -delete 0 +swap -alpha off -compose copy_opacity -composite final.png
I load the image into a hex editor and do not see the PLTE/tRNS chunks, which is probably a recap of what Drarakel just stated -- is there a further IM command to introduce these chunks into the image?
After I subsequently run pngQuant (optimizer), the image works perfectly.
I would prefer to only use IM for my batch processing once I have the process down.