Advertisement
dinel

Copy the keywords from KPA database to the file

Feb 2nd, 2012
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.97 KB | None | 0 0
  1. '''
  2.    @author: Constantin Orasan (dinel@dinel.org.uk)
  3.    @date: 2 Feb 2012
  4.    @copyright: feel free to use it and modify it as needed, but I take no responsibility for
  5.    what happens to your photo gallery :)  
  6. '''
  7.  
  8. import libxml2
  9. import os
  10. import shutil
  11. import sys
  12. import pyexiv2
  13. import string
  14.  
  15. '''
  16.    The program takes 2 arguments.
  17.    - location of the index.xml generated by KPA
  18.    - the name of the person
  19. '''
  20.  
  21. if len(sys.argv) != 3:
  22.     print "Usage python export-person-photo.py <path_to_collection> <name_of_person>"
  23.     sys.exit()
  24.  
  25. root_collection = sys.argv[1]
  26. person_name = sys.argv[2]
  27. doc = libxml2.parseDoc(open(root_collection + 'index.xml', 'r').read())
  28.  
  29. # retrieve the nodes that contain photos with a certain person
  30. nodes = doc.xpathEval("//options/option[@name='Persons']/value[@value='%s']/../../.." % person_name.strip())
  31. for node in nodes:
  32.     date = node.xpathEval('@startDate')[0].content[:10]
  33.     file = node.xpathEval('@file')[0].content
  34.     name_file = file[file.rfind("/") + 1:]
  35.     location = node.xpathEval("options/option[@name='Locations']/value/@value")
  36.     if location:
  37.         location =  location[0].content
  38.     else:
  39.         location = "unknown"
  40.        
  41.     # retrieve the keywords
  42.     keywords = node.xpathEval("options/option[@name='Keywords']/value/@value")
  43.     list_of_keywords = []
  44.     if keywords:
  45.         for keyword in keywords:
  46.             list_of_keywords.append(keyword.content)
  47.    
  48.     # now copy the files
  49.     if not os.path.exists(location):
  50.        os.mkdir(location,  0700)
  51.        
  52.     # copy the file
  53.     new_file = location + "/" + date + "-" + name_file
  54.     print "Copying file", root_collection + file    
  55.     shutil.copyfile(root_collection + file, new_file)
  56.    
  57.     # update the IPTC information
  58.     image_meta = pyexiv2.ImageMetadata(new_file)
  59.     image_meta.read()
  60.     image_meta["Iptc.Application2.Keywords"] = list_of_keywords
  61.     image_meta.write()        
  62.    
  63. doc.freeDoc()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement