Created
November 28, 2012 17:31
-
-
Save atotic/4162723 to your computer and use it in GitHub Desktop.
Convert Picasa star ratings to XMP format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 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