Pngtricks - misusing standards for fun and profit

Pngtricks is a set of two simple Python scripts, containing a parser/writer for the top-layer of the png format, and a small application, schroedipnger, using this code for a simple gimmick.
Pngtricks has been written entirely by Sebastian Hagen and is licensed under the GNU GPL.
The current version of pngtricks is available from its git repository, available at http://git.memespace.net/git/pngtricks.git/ and git://github.com/sh01/pngtricks.git.

script files:

  • png_structures.py: Data structures for parsing a PNG file into chunks, or assembling it out of chunks. When run standalone, this script takes the filename for a png-file as first argument, and prints a list of the contained chunks.
  • schroedipnger.py: A script using PIL and png_structures.py to "blend" two images. This script expects to be run standalone with the filenames of two image files as first arguments. The images in the two files should have the same resolution.

The blending done by schroedipnger works as follows:
The resulting image is built up of pixel data from both images, containing roughly 50% (assuming equal dimensions) of the pixels from each. The colors of one of the images are significantly boosted in brightness, to the point where they are effectively indistinguishable from white for a human viewer. Schroedipnger then sets the resulting PNG image's gamma (using a gAMA PNG chunk; see RFC 2083 4.2.3 for the details) to a value sufficiently low to compensate for the upwards-shifted colors.

What this all comes down to in practice is the following:
Viewers which process gAMA chunks, such as most common webbrowsers, will markedly shift the colors of the whole image down in brightness. This results in the originally brightened image parts being recognizable, and the unbrightened parts being shifted extremely close to black.
Viewers which don't process gAMA chunks, which at the time of writing includes most non-webbrowser PNG viewers, will not shift the colors back, leaving only the non-brightened image recognizable.
Ideally, this creates one PNG file displaying as entirely different images, depending on whether the viewer respects gAMA. The schroedipnger implementation isn't ideal; the result frequently displays some artifacts, sometimes partially recognizable, from the non-dominant phase. These effects can be ameliorated by choosing source-images that don't differ too much in overall dominant colors, and don't contain large continuous areas of uniform color.

For a demonstration of the result, view the sp_forsts.png example file in both a webbrowser and a non-browser PNG viewer of your choice.

Disclaimer:
This trick is not originally my idea. I originally encountered it used in another image published on the web, wrote png_structures to figure out how it was done, and schroedipnger to duplicate the effect (which was a partial success; the system which created the original image was better at avoiding cross-phase artifacts).