21 May 2006

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


IMG_5120.JPG
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 .



8BIM#1028="IPTC"
2#0="�"
2#120#Caption="I went to California - and nothing has been the same"
2#80#Byline="gpciceri"
2#5#Image Name="After a trip in San Francisco, photo num: %s"
2#25#Keyword="california"
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

IPTC='./iptc.txt'
files = glob.glob('./*.JPG')

ipct=open(IPTC,"r").read()
for file in files:
name = file.split('.JPG')[0].split('_')[1]
title = ipct % (name,)
outf = tempfile.NamedTemporaryFile()
outf.write(title)
outf.flush()
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: