Replace transparency in PNG images with white background

PngImagemagickAlpha Transparency

Png Problem Overview


I've got some PNG images with transparency, and I need to create versions with the image layer composed onto a white background. I've tried various things with Image Magick "convert" operations, but either nothing happens at all or I get an error. I don't want to go to an intermediate JPG form because I don't want the artifacts. Of course it's easy to do this in Gimp or Photoshop or whatever, but I'd really rather script it from the command line because there are many of these things.

An example of a non-working Image Magick command is:

convert img1.png -background white -flatten img1-white.png

That results in an error.

Thanks!

Png Solutions


Solution 1 - Png

-background white -alpha remove -alpha off

Example:

convert image.png -background white -alpha remove -alpha off white.png

Feel free to replace white with any other color you want. Imagemagick documentation says this about the -alpha remove operation:

> This operation is simple and fast, and does the job without needing > any extra memory use, or other side effects that may be associated > with alternative transparency removal techniques. It is thus the > preferred way of removing image transparency.

Solution 2 - Png

This works for me:

convert -flatten img1.png img1-white.png
Documentation references:

Solution 3 - Png

Flattening image and applying background image is straight forward in ImageMagick

However, order of the commands is very important

To apply any background on a transparent image and flatten it, first apply the background than flatten it. The reverse doesn't work.

$ convert sourceimage.png -background BackgroundColor -flatten destinationimage.png

Solution 4 - Png

The only one that worked for me was a mix of all the answers:

convert in.png -background white -alpha remove -flatten -alpha off out.png

Solution 5 - Png

here's how to replace the same image in all folders in a directory with white instead of transparent:

mogrify -background white -flatten */*.png

Solution 6 - Png

Using -flatten made me completely mad because -flatten in combination with mogrify crop and resizing simply doesn't work. The official and for me only correct way is to "remove" the alpha channel.

-alpha remove -alpha off (not needed with JPG)

See documention: http://www.imagemagick.org/Usage/masking/#remove

Solution 7 - Png

The Alpha Remove section of the ImageMagick Usage Guide suggests using the -alpha remove option, e.g.:

convert in.png  -background white  -alpha remove  out.png

...using the -background color of your choosing.

The guide states: > This operation is simple and fast, and does the job without needing any extra memory use, or other side effects that may be associated with alternative transparency removal techniques. It is thus the prefered way of removing image transparency.

It additionally adds the note: > Note that while transparency is 'removed' the alpha channel will remain turned on, but will now be fully-opaque. If you no longer need the alpha channel you can then use Alpha Off to disable it.

Thus, if you do not need the alpha channel you can make your output image size smaller by adding the -alpha off option, e.g:

convert in.png  -background white  -alpha remove  -alpha off  out.png

There are more details on other, often-used techniques for removing transparency described in the Removing Transparency from Images section.

Included in that section is mention of an important caveat to the usage of -flatten as a technique for removing transparency: > However this will not work with "mogrify" or with a sequence of multiple images, basically because the "-flatten" operator is really designed to merge multiple images into a single image.

So, if you are converting several images at once, e.g. generating thumbnails from a PDF file, -flatten will not do what you want (it will flatten all images for all pages into one image). On the other hand, using the -alpha remove technique will still produce multiple images, each one having transparency removed.

Solution 8 - Png

It appears that your command is correct so the problem might be due to missing support for PNG (). You can check with convert -list configure or just try the following:

sudo yum install libpng libpng-devel

Solution 9 - Png

This is not exactly the answer to your question, but I found your question while trying to figure out how to remove the alpha channel, so I decided to add this answer here:

If you want to remove alpha channel using imagemagick, you can use this command:

mogrify -alpha off ./*.png

Solution 10 - Png

Welp it looks like my decision to install "graphics magick" over "image magick" has some rough edges - when I reinstall genuine crufty old "image magick", then the above command works perfectly well.

edit, a long time later — One of these days I'll check to see if "graphics magick" has fixed this issue.

Solution 11 - Png

I needed either: both -alpha background and -flatten, or -fill.

I made a new PNG with a transparent background and a red dot in the middle.

convert image.png -background green -alpha off green.png failed: it produced an image with black background

convert image.png -background green -alpha background -flatten green.png produced an image with the correct green background.

Of course, with another file that I renamed image.png, it failed to do anything. For that file, I found that the color of the transparent pixels was "#d5d5d5" so I filled that color with green:

convert image.png -fill green -opaque "#d5d5d5" green.png replaced the transparent pixels with the correct green.

Solution 12 - Png

I saw this question and answers which really help me but then I was needed to do it for a lot of files, So in case you have multiple images (PNG images) in one folder and you want to do it for all:

find ./ -name "*.png" -exec convert {} -flatten {} \;

Solution 13 - Png

this creates an image just placing the 1st with transparency on top of the 2nd

composite -gravity center ImgWithTransp.png BackgroundSameSizeOfImg.png ResultImg.png

originally found the tip on this post

Solution 14 - Png

To actually remove the alpha channel from the file, use the alpha off option:

convert in.png -background white -alpha off out.png

Solution 15 - Png

Tried all, none worked. This one did:

convert input.png -channel rgba -alpha set \
            -fill none -opaque white \
            -fill white -opaque black \
            -fill white -opaque none \
            -alpha off output.png

Solution 16 - Png

It's -alpha off, NOT -alpha remove! iOS app store upload fails when there is an alpha channel in any icon!!

Here's how to do it: mogrify -alpha off *.png

Solution 17 - Png

This does the job for me:

magick convert OLD.png -background white -alpha remove NEW.png

Here is a starter image with a transparent background in case it helps with testing:

image with transparent background

Also, for one-offs on PC, you can always open the PNG file in Windows Paint and click Save. This will automatically turn the transparency to opaque white.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPointyView Question on Stackoverflow
Solution 1 - PngRok KraljView Answer on Stackoverflow
Solution 2 - PngremosuView Answer on Stackoverflow
Solution 3 - PngacpmasqueradeView Answer on Stackoverflow
Solution 4 - Pngdb0View Answer on Stackoverflow
Solution 5 - PngJulianView Answer on Stackoverflow
Solution 6 - PngTjarko RikkerinkView Answer on Stackoverflow
Solution 7 - PngGaryView Answer on Stackoverflow
Solution 8 - PngAlastairView Answer on Stackoverflow
Solution 9 - PngFreeNicknameView Answer on Stackoverflow
Solution 10 - PngPointyView Answer on Stackoverflow
Solution 11 - PngMcNultyView Answer on Stackoverflow
Solution 12 - PngtalsibonyView Answer on Stackoverflow
Solution 13 - PngAquarius PowerView Answer on Stackoverflow
Solution 14 - PngNick DowellView Answer on Stackoverflow
Solution 15 - PngAlexView Answer on Stackoverflow
Solution 16 - PngrailsfanaticView Answer on Stackoverflow
Solution 17 - PnghbereView Answer on Stackoverflow