PNG metadata from the command line, again

29 Aug 2007

You may have noticed that ImageMagick's identify -verbose output is huge, and if the key or data part is a bit long, the formating is all wacky. Here's a quick and dirty python script that uses PIL to print PNG metadata to the command line. It's simple enough that even if you don't know python you should be able to hack it to do what you want. Save this file as pngmeta, and then do a chmod a+x pngmeta. Then it should work just like any other shell command, e.g. ./pngmeta file1 file2 file3.... This might work with other image types as well.

#!/usr/bin/env python                                                                                                                   

# public domain, nick galbreath
# http://blog.modp.com/2007/08/png-metadata-from-command-line-again.html

import sys
from PIL import Image

# These are not user-added meta data, skip                                                                                              
reserved = ('interlace', 'gamma', 'dpi', 'transparency', 'aspect')

# sys.argv[0] is the name of the program.. skip it                                                                                      
# for each file on the command line
for file in sys.argv[1:]:
    print file
    im = Image.open(file)
    for k,v in im.info.iteritems():
        # if auto-generated metadata, skip it
        if k in reserved: continue
        print k + " = " + v

Oh great, I'm becoming a PNG metadata expert. Just what I always wanted to be.


Comment 2008-11-22 by None

Only too bad it is so hard to get in touch with when someone finally needs an PNG metadata expert.

Have you made any more improvements to writing metadata from the command line?