File tree 3 files changed +31
-0
lines changed
3 files changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ from PIL import Image
2
+
3
+
4
+ def test_getxmp ():
5
+ with Image .open ("Tests/images/xmp_test.jpg" ) as im :
6
+ xmp = im .getxmp ()
7
+
8
+ assert isinstance (xmp , dict )
9
+ assert xmp ["Description" ]["Version" ] == "10.4"
Original file line number Diff line number Diff line change @@ -528,6 +528,7 @@ def __init__(self):
528
528
self .readonly = 0
529
529
self .pyaccess = None
530
530
self ._exif = None
531
+ self ._xmp = None
531
532
532
533
def __getattr__ (self , name ):
533
534
if name == "category" :
@@ -1339,6 +1340,27 @@ def getexif(self):
1339
1340
1340
1341
return self ._exif
1341
1342
1343
+ def getxmp (self ):
1344
+ """
1345
+ Returns a dictionary containing the xmp tags for a given image.
1346
+ :returns: XMP tags in a dictionary.
1347
+ """
1348
+
1349
+ if self ._xmp is None :
1350
+ self ._xmp = {}
1351
+
1352
+ for segment , content in self .applist :
1353
+ if segment == "APP1" :
1354
+ marker , xmp_tags = content .rsplit (b"\x00 " , 1 )
1355
+ if marker == b"http://ns.adobe.com/xap/1.0/" :
1356
+ root = xml .etree .ElementTree .fromstring (xmp_tags )
1357
+ for element in root .findall (".//" ):
1358
+ self ._xmp [element .tag .split ("}" )[1 ]] = {
1359
+ child .split ("}" )[1 ]: value
1360
+ for child , value in element .attrib .items ()
1361
+ }
1362
+ return self ._xmp
1363
+
1342
1364
def getim (self ):
1343
1365
"""
1344
1366
Returns a capsule that points to the internal image memory.
You can’t perform that action at this time.
0 commit comments