Command Line Tool for Transparent PNGs


Photo of a spider. Converted to transparent GIF by command line tool Transparency.
Transparency is a tool for special, typical tasks performed on transparent PNGs, especially in the context of the WWW when you finally want GIFs instead of PNGs.

It currently supports some typical, specialised operations you can easily do with The Gimp or similar applications, but which are likely to be needed in scripts, so this is a command line tool.

I've implemented typical things I often need that ImageMagick does not provide.

Currently, two major algorithms are available, along with a few minor ones.

Original Image
Original image
Made Transparent
White made transparent

Convert White to Transparency

I needed this for scripted generation of transparent images showing rendered text. The rendered text usually comes out black-on-white, which this tool converts to black-on-transparent.

Note that this does not merely replace all white pixels by transparent ones, but it does a full shading using the grey value of each pixel: it installs a new alpha channel from the grey values. (Gimp: install layer mask, copy the grey image into it, invert, apply layer mask).

This is not equivalent to pnmtopng -transparent white.

Assuming red background
Alpha applied assuming red background
Used on red background
How the GIF looks on red background

Perfectly Convert PNG to GIF

PNG features a full-fledged alpha channel, making half-transparent images easy to handle. However, not all web browsers support it the alpha channel of PNGs, so I wanted GIF images on my web site, but PNG images as my working copy.

This application prepares PNGs for conversion to GIF by making all pixels either opaque or fully transparent. When you provide a background colour, the resulting GIF looks just as smoothly embedded as the original PNG. This works particularly well for antialiased pictures or text.

Combination

You will often want to combine the two steps above and might want to additionally scale the image. You can do this in one step with Transparency.


Example

Imagine you have a scanned image of something printed on paper and want to make it transparent for your web page. The original image might look like this:

'Aromator' before

Now assume you have that image as PNG and want it smaller, transparent, and as GIF. First, convert white to transparency and scale:

'Aromator' semitransparent, in editor
The PNG looks like this in Gimp
'Aromator' semitransparent, on blue
PNGs adjust to any background. For GIF,
you'll need to know the colour in advance.

Transparency can prepare a new PNG that converts directly to GIF. You need to know the background colour, though. Assume it is #c0c0c0, then you can do in one step:

transparency aromator1.png \
    --white-to-alpha \
    --scale=0.5 \
    --bg='#c0c0c0' \
    --apply-alpha-keep='#1' \
    aromator2.gif

The last step needs clarification: apply-alpha-keep='#1' applies the alpha channel using the given background colour, but keeps the transparency were the picture is opaque by no more than 1/15 (specified on a single digit greyscale #0..#f hexadecimal). Transparency is culled: pixels < 1/15 opacity become fully transparent, all others become fully opaque.

Viewed in an image editor, the result will show up as follows. You can see the transparency, and also how the antialiasing to the background is still part of the opaque areas of the image (you can see the frame around the letters):

'Aromator' after, in editor
Either full transparency is kept or else
the alpha channel is applied.
'Aromator' after, on blue
Fully opaque or fully transparent,
as needed for GIF.

Converted to GIF (e.g. with ImageMagick), this looks as follows in your browser:

'Aromator' after, in browser


Source Code

LibError is required for compilation:


Changes

  • Version 0.1.149

    • Clean-up: made compile with gcc 4.0
  • Version 0.1.148 (no official release)

    • Bug fix (hopefully): compilation under Windos. Please report success or failure!
    • Code downgrade: make compile with old MS compiler with broken for() scope.
  • Version 0.1.144

    • Bug fix: gcc 3.4+ compilation
    • Bug fixes: newest Erwin used to support 64-bit machines
  • Version 0.1.141

    • Bug fix: reading of paletted images should work now
    • Feature: Cropping by given coordinates
    • Feature: Auto-cropping in X direction
    • Feature: GIF can be written directly. I mainly did this because my version of ImageMagick produces ridiculously large GIF files. No quantize algorithm is implemented, so the number of colours must be limited to 255/256, depending on whether transparency exists. ImageMagick can quantize (and now, the program can read back those paletted images...).
    • Bug fix in the GIF writer.
  • Version 0.1.86

    • Bug fix: Erwin had a bug fix, so the instatiated files had to be repackaged: on some machines, v_char.cpp would not compile.
  • Version 0.1.82

    • Initial Release
bar project at Freshmeat
dot2gdl project at Freshmeat
erwin project at Freshmeat
liquid project at Freshmeat
badut project at Freshmeat
pamlirc project at Freshmeat
liberror project at Freshmeat
logfiles project at Freshmeat
parrot project at Freshmeat
rushhour project at Freshmeat
transparency project at Freshmeat

Index

August 28th, 2008
Comments? Suggestions? Corrections? You can drop me a line.
zpentrabvagiktu@theiling.de
Schwerpunktpraxis