Page 1 of 1
Converting PNGs with transparency to JPG leads to buggy rendering
Posted: 2017-11-14T06:11:05-07:00
by xyzdragon
I have a test png which when converted to jpg results in jagged lines but only for rising lines, not for falling ones(!) if the background is transparency.
To the right you can see the original PNG (rendered with ristretto) which has white background in the left half and transparent background in the right half. To the left you can see the jpg produced by Imagemagick using
Code: Select all
convert test-lines.png -quality 100 test-lines.jpg
(Please click on the image to view it in 100%)
As you can see there is no antialiasing at all plus the bold 45° ascending diagonal on the right isn't even smooth anymore, but has a jagged edge!
All of this can be avoided by filling the background with white, e.g.:
Code: Select all
convert test-lines.png -background white -alpha remove -quality 100 test-lines.jpg
But in my opinion if it is already rendered with white background by default then the transparency shouldn't lead to this unexpected behavior!
Zoom on jagged edges:
Re: Converting PNGs with transparency to JPG leads to buggy rendering
Posted: 2017-11-14T06:16:54-07:00
by Bonzo
What version of Imagemagick are you using?
Re: Converting PNGs with transparency to JPG leads to buggy rendering
Posted: 2017-11-14T06:18:28-07:00
by xyzdragon
I'm using
Code: Select all
Version: ImageMagick 6.9.7-4 Q16 x86_64 20170114 http://www.imagemagick.org
Copyright: © 1999-2017 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC Modules OpenMP
Delegates (built-in): bzlib djvu fftw fontconfig freetype jbig jng jp2 jpeg lcms lqr ltdl lzma openexr pangocairo png tiff wmf x xml zlib
Re: Converting PNGs with transparency to JPG leads to buggy rendering
Posted: 2017-11-14T06:50:50-07:00
by snibgo
It would help if you linked to the actual PNG file, rather than a screen shot of two versions of the file.
As you probably know, JPEG can't store transparency. So the alpha channel is simply ignored when saving to JPEG. In PNG files, fully transparent pixels are usually "transparent black", though they might be "transparent anything". Anti-aliased pixels are usually the same colour as the solid colour (in your case, black or white), but with varying transparency.
So, simply ignoring alpha will turn anti-aliased pixel into the solid colour, creating aliasing.
When saving to JPEG, we often want to flatten against white. This will turn the antialiased pixels from "black or white with transparency" to "gray".
Re: Converting PNGs with transparency to JPG leads to buggy rendering
Posted: 2017-11-14T06:59:51-07:00
by xyzdragon
Actual PNG
Ah I see, yes it works with
alone without specifying the background. So it is antialiased, but all pixels with alpha!=0 are simply ignored, I see. I'm not entirely sure if the default behavior should somehow be changed, e.g. a automatic flatten. I don't think anyone would want the default result as it is now and might not even notice the problem is the transparency, because it is rendered on white background anyway.
Re: Converting PNGs with transparency to JPG leads to buggy rendering
Posted: 2017-11-14T07:21:46-07:00
by snibgo
On the right side of your PNG, you have two colours: black (with varying amounts of transparency), and fully-transparent white.
So the JPEG contains only those two colours from the PNG: black and white.
All the lines are aliased, but this is obvious only on the rising diagonal, or when you zoom in.
As a general rule, IM doesn't try to guess what the user really wants. When saving a semi-transparent image as a JPEG, IM uses the colours that are in the pixels. It doesn't guess that the user doesn't really want those colours, but the colours after flattening against a background.
Personally, I would do it lke this:
Code: Select all
convert 642919.png -background White -layers flatten x.jpg
This has the same effect as your "-alpha remove", but is more clear, to a human, what it is doing.
Re: Converting PNGs with transparency to JPG leads to buggy rendering
Posted: 2017-11-14T07:28:00-07:00
by xyzdragon
Ok, thanks for the fast help.