-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlightroomTags.py
38 lines (35 loc) · 1.65 KB
/
lightroomTags.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import xml.dom.minidom
def parse_xmp_for_lightroom_tags(xmp_string):
data = {}
data['rating'] = 0
data['tags'] = []
xmlDocument = xml.dom.minidom.parseString(xmp_string)
xmp = xmlDocument.documentElement
if xmp.hasAttribute('xmlns:x'):
if xmp.getAttribute('xmlns:x') == 'adobe:ns:meta/':
# this is adobe meta data so continue
try:
rdf = xmp.getElementsByTagName('rdf:RDF')[0]
descArray = rdf.getElementsByTagName('rdf:Description')
for desc in descArray:
ratingElement = desc.getElementsByTagName('xmp:Rating')
if len(ratingElement) > 0:
rating = ratingElement[0].firstChild.nodeValue
data['rating'] = int(rating)
subjects = desc.getElementsByTagName('dc:subject')
if len(subjects) > 0:
bags = subjects[0].getElementsByTagName('rdf:Bag')
if len(bags) > 0:
lightroomTags = bags[0].getElementsByTagName('rdf:li')
tagsCombinedArray = []
for tags in lightroomTags:
tag = tags.firstChild.nodeValue
#print(tag)
tagsCombinedArray.append(tag)
if len(tagsCombinedArray):
data['tags'] = tagsCombinedArray
except:
# no description
# print("the image has no valid ligthroom tags")
pass
return data