Replace transparency in PNG images with white background

后端 未结 16 639
再見小時候
再見小時候 2020-11-29 14:48

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 Magi

相关标签:
16条回答
  • 2020-11-29 15:26

    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.

    0 讨论(0)
  • 2020-11-29 15:27

    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
    
    0 讨论(0)
  • 2020-11-29 15:27

    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

    0 讨论(0)
  • 2020-11-29 15:27

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

    convert in.png -background white -alpha off out.png
    
    0 讨论(0)
  • 2020-11-29 15:28
    -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.

    0 讨论(0)
  • 2020-11-29 15:28

    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.

    0 讨论(0)
提交回复
热议问题