Converting PNGs with transparency to JPG leads to buggy rendering

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
xyzdragon
Posts: 4
Joined: 2017-11-14T05:55:06-07:00
Authentication code: 1152

Converting PNGs with transparency to JPG leads to buggy rendering

Post 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%)
Image
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:
Image
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Converting PNGs with transparency to JPG leads to buggy rendering

Post by Bonzo »

What version of Imagemagick are you using?
xyzdragon
Posts: 4
Joined: 2017-11-14T05:55:06-07:00
Authentication code: 1152

Re: Converting PNGs with transparency to JPG leads to buggy rendering

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Converting PNGs with transparency to JPG leads to buggy rendering

Post 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".
snibgo's IM pages: im.snibgo.com
xyzdragon
Posts: 4
Joined: 2017-11-14T05:55:06-07:00
Authentication code: 1152

Re: Converting PNGs with transparency to JPG leads to buggy rendering

Post by xyzdragon »

Actual PNG Image
Ah I see, yes it works with

Code: Select all

-alpha remove
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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Converting PNGs with transparency to JPG leads to buggy rendering

Post 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.
snibgo's IM pages: im.snibgo.com
xyzdragon
Posts: 4
Joined: 2017-11-14T05:55:06-07:00
Authentication code: 1152

Re: Converting PNGs with transparency to JPG leads to buggy rendering

Post by xyzdragon »

Ok, thanks for the fast help.
Post Reply