PNG2GIF: Convert PNG images to GIF

August 21, 2008

The PNG image file format is vastly superior to GIF, supporting many more colours and better transparency. IE6, however, doesn’t support PNG transparency without the use of a browser specific hack. Therefore the easiest way to display partly transparent images across multiple browser without the use of a hack is to use a GIF image instead.

The excellent set of free and widely used silk icons from FAMFAMFAM come as a set of over 700 PNG images, and I frequently found myself converting a few icons from PNG to GIF for a web project. Converting more than one or two images at a time quickly gets boring, so I created a Python command line utility, based on the code at Nadia Alramli’s blog, to perform the conversion automatically.

Creating GIF files from all the PNG images in the current directory becomes as simple as:

png2gif *.png

If you want to create the GIF images, and delete the PNG images then:

png2gif -r *.png

And if you’d like to output the GIF images to a different directory then:

png2gif -o gifImages/ *.png

To view a full list of supported options you can run png2gif with -h, which shows:

Usage: png2gif [OPTIONS] <files>

Convert PNG images to GIF format

Options:
-h, --help            show this help message and exit
-o OUTPUTDIR, --outputdir=OUTPUTDIR
Set the output directory in which to put the GIF
images. Defaults to the current directory
-t THRESHOLD, --threshold=THRESHOLD
Set the transparency threshold. Defaults to 0
-r, --replace         Delete the PNG files after converting them to GIF
-v, --verbose         Verbose output

You can download the utility here: png2gif

And you can also download the silk icons in GIF format, as converted by PNG2GIF.

4 Comments »

  1. The little-known PNG8 variant is transparent on IE6, and superior to GIFs.

    I blogged about a nasty deadlock problem with the CSS alpha transparency filter hack at http://blogs.cozi.com/tech/2008/03/transparent-png.html

    Comment by George Reilly — August 25, 2008 @ 3:01 am

  2. Thanks Ben!

    It really helped me a lot.

    Comment by Arif — November 16, 2008 @ 4:14 pm

  3. Hi.

    Good script. Nice work. I suggest You to mention about python library requirements (import Image) is on some systems (e.g. debian) it’s installed by default with python interpreter.

    I d/l the script, but it is not working out-of-the box. Belowe are my fixes [diff between Yours original and fixed working file]. Html escaping removed and the most important – fixed missing of first argument file.

    Fixes:

    35c35
    if alpha <= threshold:
    80c80
    usage = ‘png2gif [OPTIONS] ‘)
    90c90
    if options.verbose: print “Creating output directory”, options.outputdir
    98c98
    for i in range(0, len(arguments)):

    Comment by brajek — October 7, 2009 @ 2:10 pm

  4. Diff is not shown properly in HTML. Can’t help it.

    Comment by brajek — October 7, 2009 @ 2:11 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment