Skip to content

Instantly share code, notes, and snippets.

@lukas-hetzenecker
Created January 20, 2016 04:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukas-hetzenecker/1cc07f05b9a45e3e7255 to your computer and use it in GitHub Desktop.
Save lukas-hetzenecker/1cc07f05b9a45e3e7255 to your computer and use it in GitHub Desktop.
#!/home/lukas/projects/virtualenv-photostation/bin/python
import os
import sys
import argparse
from libxmp import consts, XMPFiles, XMPError
from difflib import unified_diff
parser = argparse.ArgumentParser(description='Resync image tags.')
parser.add_argument('folders', metavar='FOLDER', type=str, nargs='*',
help='folders that are parsed')
parser.add_argument('--write', dest='write', action='store_true')
parser.add_argument('--no-write', dest='write', action='store_false')
parser.set_defaults(write=False)
parser.set_defaults(folders=['/files/photos'])
args = parser.parse_args()
map_lr_dk = {
'Red': '1',
'Orange': '2',
'Yellow': '3',
'Green': '4',
'Blue': '5',
'Magenta': '6',
'Gray': '7',
'Black': '8',
'White': '9'
}
map_dk_lr = {v: k for k, v in map_lr_dk.items()}
for base in args.folders:
for root, dirs, files in os.walk(base):
print('Entering directory %s' % root)
for file in files:
if file.lower().endswith('.jpg'):
try:
xmpfile = XMPFiles(file_path=root + os.sep + file, open_forupdate=True)
except Exception as e:
print("Error handling file ", root + os.sep + file)
raise e
xmp = xmpfile.get_xmp()
try:
xmp.register_namespace('http://www.digikam.org/ns/1.0/', 'digikam')
except XMPError:
pass
try:
lr = xmp.get_property(consts.XMP_NS_XMP, 'Label')
except XMPError:
lr = None
try:
dk = xmp.get_property('http://www.digikam.org/ns/1.0/', 'ColorLabel')
except XMPError:
dk = None
if dk == '0':
dk = None
if lr is None and dk is None:
continue
if lr is not None and lr not in map_lr_dk:
print("ERROR! Lightroom color label '%s' could not be mapped!" % lr)
continue
if dk is not None and dk not in map_dk_lr:
print("ERROR! Digikam color label '%s' could not be mapped!" % dk)
continue
before = str(xmp)
if lr == 'White' or dk == '9':
xmp.delete_property('http://www.digikam.org/ns/1.0/', 'ColorLabel')
xmp.delete_property(consts.XMP_NS_XMP, 'Label')
elif lr is not None and map_lr_dk[lr] == dk:
continue
elif lr is not None and map_lr_dk[lr] != dk:
xmp.set_property('http://www.digikam.org/ns/1.0/', 'ColorLabel', map_lr_dk[lr])
elif dk is not None:
xmp.set_property(consts.XMP_NS_XMP, 'Label', map_dk_lr[dk])
after = str(xmp)
diff = unified_diff(before.splitlines(keepends=True), after.splitlines(keepends=True),
fromfile=root + os.sep + file)
print(''.join(diff), end="")
if args.write:
if xmpfile.can_put_xmp(xmp):
xmpfile.put_xmp(xmp)
xmpfile.close_file()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment