Sunday, August 19, 2007

Editing PNG Metadata from the Command Line

You would think in our Web 2.0 universe with it's tagoholics, that PNG image metadata would be used and easy to use. Oddly it's not. I know everyone is trying to make their PNG files smaller, but a few bytes describing the owner and copyright information isn't going to kill anyone. Very few (if any) image APIs and GUI applications let you add or edit the metadata and many just erase any that exist.

Here's some tips on read and write PNG metadata from the command line.

PNG Metadata and ImageMagick

Here's how to manipulate metadata to PNG files using Image Magick.

How to read metadata
identify -verbose FILE.png

This spits out all metadata, user defined or otherwise.

How to write metadata
convert INFILE.png -set Title "foobar the great" OUTFILE.png

INFILE and OUTFILE can be the same.

Ugly Details

ImageMagick can read tTXt and zTXt but I was unable to get iTXt to work. It could be that I was not writing the iTXt correctly and I'm too lazy to look at their source code. I'm also told that the libpng does support iTXt yet.

While the PNG spec says you can add chunks in just about any order. In practice, you should put the tEXt chunks before the image data. If you don't, ImageMagick appears to ignore them.

Reading PNG Metadata and PIL

The Python Imaging Library allows you to read but not write metadata(see below). In fact, if you save the image, all metadata will be erased.

http://www.blogger.com/img/gl.link.gif
$ python
Python 2.5 (r25:51908, Oct  7 2006, 01:04:15) 
Type "help", "copyright", "credits" or "license" for more information.
>>> import Image
>>> img = Image.open("junk.png")
>>> img.info
{'foo': "bar"}

Also, PIL cannot read zTXt or iTXt chunks. Boo.

UPDATE 28-Aug-2007: PIL 1.6 secretly allows you to add metadata. See this post for details.

PNG Metadata and PNGCrush

PNGCrush is a nifty little program for optimizing the size of PNG files. It also let's you write metadata. But sadly, I think it has a little bug.

A sister program pngmeta dumps metadata. It's old but seems to work. It does not let you add metadata.

1 comments:

Luke said...

Interesting blog, I have been googling for a simple metadata editor for PNG files, but cannot find one. Strange I would have assumed that adding a GUI to the terminal commands would be easy.

Hopefully someone will do this soon.