Replace transparency in PNG images with white back

2019-01-09 23:37发布

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!

15条回答
戒情不戒烟
2楼-- · 2019-01-09 23:47

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.

查看更多
太酷不给撩
3楼-- · 2019-01-09 23:48

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

convert in.png -background white -alpha off out.png
查看更多
劫难
4楼-- · 2019-01-09 23:48

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

查看更多
迷人小祖宗
5楼-- · 2019-01-09 23:51

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
查看更多
家丑人穷心不美
6楼-- · 2019-01-09 23:52

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

mogrify -background white -flatten */*.png

查看更多
仙女界的扛把子
7楼-- · 2019-01-09 23:56

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

查看更多
登录 后发表回答