Sunday, August 14, 2011

Transparent PNG (or GIF) From the Command Line

Given the popularity of my post about making transparent PNG images with the Gimp, I should share a recent discovery: If you know the exact color you want to make transparent, you can do it from the command line.

What you need is the ImageMagick package, picture manipulation software that's available for all major Linux distributions, Windows, and Mac OS X (through MacPorts). If none of those work for you, you can also compile the source code.

Specifically, you need the convert command line tool. The syntax is:
convert -transparent color original_picture.png picture_with_transparent_background.png
and you can covert from png to gif, or visa versa, with the same command.

An example: here's the good ol' pengjay, with a horrid green (#00FF00) background:

Pengjay with green background

Since we know the background color is #00FF00, we can pipe it through convert:

convert -transparent #00FF00 pengjay_green.png pengjay_transparent.png

and we get this:

Pengjay with transparent background

Note that if you don't know the color, you're better off using the GIMP.


Anonymous said...

Can you set it to anything other than full transparency? What if I want the alpha channel to be 127?

rcjhawk said...

$ man convert
doesn't list that as an option. Looking at display, the main program in the ImageMagick package, doesn't seem to have that option. For this you need something like the Gimp.

Rose Diana said...

thank you sharing!

Melbourne SEO Services