Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- @author: Constantin Orasan (dinel@dinel.org.uk)
- @date: 2 Feb 2012
- @copyright: feel free to use it and modify it as needed, but I take no responsibility for
- what happens to your photo gallery :)
- '''
- import libxml2
- import os
- import shutil
- import sys
- import pyexiv2
- import string
- '''
- The program takes 2 arguments.
- - location of the index.xml generated by KPA
- - the name of the person
- '''
- if len(sys.argv) != 3:
- print "Usage python export-person-photo.py <path_to_collection> <name_of_person>"
- sys.exit()
- root_collection = sys.argv[1]
- person_name = sys.argv[2]
- doc = libxml2.parseDoc(open(root_collection + 'index.xml', 'r').read())
- # retrieve the nodes that contain photos with a certain person
- nodes = doc.xpathEval("//options/option[@name='Persons']/value[@value='%s']/../../.." % person_name.strip())
- for node in nodes:
- date = node.xpathEval('@startDate')[0].content[:10]
- file = node.xpathEval('@file')[0].content
- name_file = file[file.rfind("/") + 1:]
- location = node.xpathEval("options/option[@name='Locations']/value/@value")
- if location:
- location = location[0].content
- else:
- location = "unknown"
- # retrieve the keywords
- keywords = node.xpathEval("options/option[@name='Keywords']/value/@value")
- list_of_keywords = []
- if keywords:
- for keyword in keywords:
- list_of_keywords.append(keyword.content)
- # now copy the files
- if not os.path.exists(location):
- os.mkdir(location, 0700)
- # copy the file
- new_file = location + "/" + date + "-" + name_file
- print "Copying file", root_collection + file
- shutil.copyfile(root_collection + file, new_file)
- # update the IPTC information
- image_meta = pyexiv2.ImageMetadata(new_file)
- image_meta.read()
- image_meta["Iptc.Application2.Keywords"] = list_of_keywords
- image_meta.write()
- doc.freeDoc()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement