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

transparency-0.1.151-src.tar.bz2
transparency-0.1.151-src.tar.bz2.md5
transparency-2023-01-17.tgz

LibError is required for compilation before v2023-01-17:

liberror-2.1.89882.tgz
liberror-2.1.89882.tgz.md5


Changes

Index

January 17th, 2023
Comments? Suggestions? Corrections? You can drop me a line.
zpentrabvagiktu@theiling.de
Schwerpunktpraxis
Datenschutz