Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace transparency in PNG images with white background

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!

like image 419
Pointy Avatar asked Feb 24 '10 00:02

Pointy


People also ask

How do I change the transparency of a PNG?

If you want to change the opacity of a smaller area than the entire PNG, you can select a rectangular area of the PNG and apply the opacity effect only in this region. The opacity region can be changed in the preview with your mouse or in the options using the left/right offsets and width/height parameters.


1 Answers

-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.

like image 70
Rok Kralj Avatar answered Oct 08 '22 17:10

Rok Kralj