#!/usr/bin/python # # Extracts jpeg comments from jpegs and update an Albumshaper # XML file with them. # # 1. add JPEG comments (digikam, whatever) # 2. add images to an Albumshaper album as usual, save, exit # 3. 'cd' to album top directory and run this script # # Redirect to file to obtain results # # WARNING: overwrites any previous comments # # v1.0; Andras.Horvath@cern.ch, 02/aug/2006 import os import Image import md5 from xml.dom import ext from xml.dom.ext.reader import PyExpat map={} def parse_xml_from_file(fileName): #build a DOM tree from the file reader = PyExpat.Reader() doc = reader.fromUri(fileName) for i in doc.getElementsByTagName("photo"): # we assume these exist :) desc=i.getElementsByTagName("description")[0]; img=i.getElementsByTagName("image")[0]; sum=img.getElementsByTagName("md5")[0].firstChild.nodeValue; if desc.hasChildNodes(): desc.firstChild.nodeValue=map[sum] else: desc.appendChild(doc.createTextNode(map[sum])) ext.Print(doc) #reclaim the object reader.releaseNode(doc) def read_comments(): jpglist=os.popen("/usr/bin/find . -name '*.jpg' ! -name '*thumb*' ! -name '*slideshow*'") for i in jpglist.readlines(): n=Image.open(i[:-1]) # minus \n cmnt=n.app.get("COM","").rstrip("\n\r ") sum=md5.new(open(i[:-1]).read()).hexdigest() map[sum]=cmnt if __name__ == '__main__': import sys # FIXME: check if Album.xml exists, here read_comments() parse_xml_from_file("Album.xml")