Editing PNG Metadata from the Command Line

19 Aug 2007

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.

$ 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.

Comment 2008-06-17 by None

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.

Comment 2009-04-03 by None

Exactly the info I was looking for. Thanks.

Comment 2009-06-02 by None

This comment has been removed by a blog administrator.

Comment 2010-03-03 by None

Thanks a lot - I also need to edit the metadata of a large set of png images.
However, I cannot get the above commands to preserve the sorting of the text fields. ImageMagick sorts them alphabetically and Python also scrambles the order. Any ideas?

Comment 2010-03-03 by None

@carl -- it's unlikely any of these solutions will preserve order. If you need this, adding a prefix to your keys/fields will allow you with a quick sort to order them correctly. (e.g. "00-first", "01-apple", "02-dog", or "TIMESTAMP-name"). Good luck.