21 May 2006

And the winner is... (writing IPTC metadata into a jpeg)

Originally uploaded by gpciceri.
Yes, I've experimented a little, but at least I'm able to annotate my pictures with the EXIF/IPTC metadata that dreamstime's upload system tries to read when you sumbit a photo - all of this without opening each photo by hand but using a batch procedure (thanks to Imagemagick's mogrify utility).

First of all the metadata: save them in a file named ./iptc.txt .

2#120#Caption="I went to California - and nothing has been the same"
2#5#Image Name="After a trip in San Francisco, photo num: %s"
2#25#Keyword="san francisco"

The meaning of these fields are obvious, isn't it ?

And then the python script that rules it all (courtesy of my left brain):

import glob, subprocess, tempfile, os

files = glob.glob('./*.JPG')

for file in files:
name = file.split('.JPG')[0].split('_')[1]
title = ipct % (name,)
outf = tempfile.NamedTemporaryFile()
print "processing:", file, name
p=subprocess.Popen("mogrify -profile 8BIMTEXT:" + outf.name + " " + file, shell=True)
sts = os.waitpid(p.pid, 0)

It reads the metadata to add from the previous file, cycles over the *.JPG file in the current directory, substitutes the image number in the image name and mogrify the file - writing the metadata into it. Et voilà.

No comments: