6

Edit: The original title of this question was "Drawbacks of using -flatten option". However, after this question was answered, I decided to change its title, to make it easier found on Google. Also, new title is actually better describes what is told below.

As I discovered today, to convert PNG with transparency into JPG, we need to use -flatten option.

Try it yourself: download Google logo and convert it from PNG to JPG with the following line

convert google.png google.jpg

the picture will be messed:

enter image description here

With -flatten option, it works fine:

convert google.png -flatten google.jpg

I'm wondering are there any drawbacks of using -flatten permanently, for all conversions between PNG, JPG/JPEG and GIF.

john c. j.
  • 725
  • 5
  • 28
  • 81

2 Answers2

8

The problem with converting PNG to JPG is when the PNG has transparency. JPG does not allow transparency and any transparent areas will show what color is underneath the transparency, which is often black. So you should use -flatten to properly do that conversion. But you should specify -background somecolor before -flatten, if you do not want the default background color. GIF only allows binary transparency -- fully transparent or fully opaque. PNG allows 8-bit transparency (partial transparent). I know of no significant issues using -background xx -flatten when converting PNG or GIF to JPG. However, the background color you use will change the appearance in transparent areas from that of the underneath color. Here is what is happening:

Input:

enter image description here

Turn alpha off:

convert google.png -alpha off google_aoff.jpg

enter image description here

The stripes are from the underneath color below the alpha channel.

Alpha Channel (nicely antialiased):

convert google.png -alpha extract google_alpha.jpg

enter image description here

Simple Flatten (default background is white):

convert google.png -flatten google_flatten.jpg

enter image description here

Flatten with black background:

convert google.png -background black -flatten google_flatten_black.jpg

enter image description here

Often one will reprocess the original transparent PNG image so that it has some constant color underneath the alpha channel so that later one can remove the alpha channel and not have odd colors showing. It will look the very same as the original PNG.

convert google.png -background white -alpha background google_bg_white.png

enter image description here

However, if you simply remove the alpha channel the JPG will show aliasing since only the fully transparent pixels' background colors were changed to white. You have a nice clean background, but the image is still aliased (as it was in the original when the alpha channel was remove).

convert google_bg_white.png google_bg_white.jpg

enter image description here

So one still needs to flatten the result, so that the antialiasing of the alpha channel will smoothly blend the colors near the boundaries.

convert google_bg_white.png -flatten google_bg_white_flatten.jpg

enter image description here

An alternate method to -flatten is to use -alpha remove, which is discussed http://www.imagemagick.org/Usage/masking/#alpha_remove. So starting with the original PNG, we do

convert google.png -background white -alpha remove google_alpharemoveoff.jpg

enter image description here

The result is the same as -background white -flatten. We do not need the -alpha off mentioned in the reference, since JPG does not support any alpha channel. The reference says this is more efficient and is the preferred method.

fmw42
  • 46,825
  • 10
  • 62
  • 80
  • Thank you very much. One thing that I did not understand: *"Often one will reprocess the original transparent PNG [...] You may note that the PNG with modified white background under the transparency shows a bit of aliasing. But it goes way when the alpha channel is removed when simply converting to JPG."* Are there any advantages of this 2-step approach over simply using `-flatten`? – john c. j. Dec 23 '17 at 23:56
  • `@John C`. I was surprised about that aliasing, too. So I reviewed my post and found that I posted the wrong image. The correct image does not have aliasing, though the aliasing issue still needed to be discussed. So I have edited my Answer to correct that image, explain about the aliasing (in the original image) and also show an alternate method to -flatten. – fmw42 Dec 24 '17 at 01:33
  • Thank you again :) So, as I understand there are 3 ways to convert PNG (with transparency) into JPG. I posted it here: https://pastebin.com/raw/RMN0tVp2. Is it all correct? – john c. j. Dec 25 '17 at 15:03
  • Fred, do you know of some benefit to creating the image the way Google have chosen to? It seems an odd thing to do... – Mark Setchell Dec 26 '17 at 15:09
  • 1
    `@Mark Stechell`. No I do not know why they do it that way. I see that often from Photoshop created images, also. Personally, I would create a transparent background and then draw the letters in the desired colors. But perhaps their tools don't work that simply. – fmw42 Dec 26 '17 at 18:42
1

@John C wrote:

1st approach:
convert google.png -flatten google_flatten.jpg   

2nd approach:
convert google.png -background white -alpha background google_bg_white.png    
convert google_bg_white.png -flatten google_bg_white_flatten.jpg

3rd approach:
convert google.png -background white -alpha remove google_alpharemoveoff.jpg

More properly, these should be

1st approach
convert google.png -background white -flatten google_flatten.jpg

2nd approach
convert google.png -background white -alpha background -flatten google_bg_white_flatten.jpg

3rd approach
convert google.png -background white -alpha remove -alpha off google_alpharemoveoff.jpg


In case 1: -background white is the default. But if you want some other background color you need to specify it.

In case 2: there is no need to save to an intermediate file

In case 3: you will need -alpha off if you save to PNG. JPG does not support transparency, so turning alpha off is not needed.

john c. j.
  • 725
  • 5
  • 28
  • 81
fmw42
  • 46,825
  • 10
  • 62
  • 80