Skip to content

Instantly share code, notes, and snippets.

@jterhune
Forked from atotic/PicasaToXMP.py
Last active September 30, 2018 23:05
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jterhune/4444862 to your computer and use it in GitHub Desktop.
Save jterhune/4444862 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# prerequisite: http://www.exiv2.org/download.html
import sys
import os.path
import re
import subprocess
def process_ini(fname):
# print "found ini " + fname + "\n"
dirname = os.path.dirname(fname)
f = open(fname,"r")
curfile = ""
for line in f.readlines():
line = line.rstrip()
match = re.search(r'^\[(.*)\]$', line, re.M)
if match:
curfile = dirname + "/" + match.group(1)
# print "found file name " + curfile
else:
match = re.search(r'^star=yes$', line, re.M)
if match:
# exiv2 -M"add Xmp.xmp.Rating XmpText 4"
# print "found star file " + curfile + "\n"
retVal = subprocess.call(['exiv2', '-Madd Xmp.xmp.Rating XmpText 5', curfile])
if retVal != 0:
write_error(curfile +"\t error writing Rating metadata")
match = re.search(r'^keywords=(.*)$', line, re.M)
if match:
keywords = match.group(1)
for keyword in keywords.split(","):
retVal = subprocess.call(['exiv2', '-Mset Xmp.dc.subject ' + keyword, curfile])
if retVal != 0:
write_error(curfile +"\t error writing subject metadata")
f.close()
def process_dir(dir):
print "Processing ", dir
if os.path.basename(dir) == "Originals" or os.path.basename(dir) == ".picasaoriginals":
return
if os.path.exists(dir + "/" + ".picasa.ini"):
process_ini(dir + "/" + ".picasa.ini")
elif os.path.exists(dir + "/" + "Picasa.ini"):
process_ini(dir + "/" + "Picasa.ini")
for filename in os.listdir(dir):
if os.path.isdir(dir + "/" + filename):
process_dir(dir + "/" + filename)
def write_error(str):
global ERRFILE, errfile_path
if ERRFILE == 0:
ERRFILE = open(errfile_path, 'w')
if ERRFILE != 0:
ERRFILE.write(str + "\n")
# setup
homedir = "."
if len(sys.argv) == 2:
homedir = sys.argv[1];
elif len(sys.argv) != 1:
print "usage: PicasaToXMP.py [directory]"
homedir = os.path.realpath(homedir)
# errors
ERRFILE = 0;
errfile_path = homedir + "/" + "PicasaToXMP.ERROR.txt"
# execute
process_dir(homedir)
if ERRFILE != 0:
ERRFILE.close();
print "Errors encountered. Error log saved in:\n" + errfile_path + "\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment