Created
July 17, 2016 23:44
-
-
Save kamermans/0b113bda0a5b6b2808f99b72c228fcec to your computer and use it in GitHub Desktop.
Windows Metadata Comments to Adobe Lightroom Sync
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/env python2 | |
# | |
# exif_comment_to_lightroom.py | |
# | |
# Windows Metadata Comments => Adobe Lightroom Sync | |
# | |
# This script will decode and copy the comment field from the Windows metadata details | |
# (when you right-click on an image and go to details in Windows Explorer) into the | |
# Adobe Lightroom compatible IPTC XMP caption field. After running, select all affected | |
# images and select Metadata -> Read Metadata from File. | |
# | |
# Note that although this worked fine for me, your milage may vary. Also, this may overwrite | |
# your Lightroom metadata, although it is supposed to only alter the caption field. | |
# | |
# To use this script, first install Python 2.x, then run: | |
# pip install piexif | |
# pip install IPTCInfo | |
# | |
# Then update the images_dir and run this script. All files that can be read will be updated. | |
# | |
# Author: Steve Kamerman, 2016 | |
# | |
from os import listdir | |
from os.path import isfile, join | |
from iptcinfo import IPTCInfo | |
import piexif | |
images_dir = "c:/Users/My User/Pictures" | |
filenames = [f for f in listdir(images_dir) if isfile(join(images_dir, f))] | |
for filename in filenames: | |
image_path = join(images_dir, filename) | |
try: | |
exif = piexif.load(image_path) | |
except: | |
print "Unable to open %s" % filename | |
continue | |
if piexif.ImageIFD.XPComment not in exif["0th"]: | |
#print "Error: No comment" | |
continue | |
comment_bytes = exif["0th"][piexif.ImageIFD.XPComment] | |
comment_bytes = [a for a in comment_bytes if a != 0] | |
comment = "".join(map(chr, comment_bytes)) | |
try: | |
iptc = IPTCInfo(image_path, force=True) | |
except: | |
print "Error initializing IPTC data" | |
continue; | |
if comment != iptc.data['caption/abstract']: | |
print "=> Updating file '%s' with comment: \n%s" % (filename, comment) | |
iptc.data['caption/abstract'] = comment | |
iptc.save() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment