diff data/plugin/macro/MoinImage.py @ 264:b7e7be4340b0

MoinImage: add IPTC support
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 13 Sep 2008 22:26:31 +0200
parents 448a086d840c
children
line wrap: on
line diff
--- a/data/plugin/macro/MoinImage.py	Sat Sep 13 20:21:20 2008 +0200
+++ b/data/plugin/macro/MoinImage.py	Sat Sep 13 22:26:31 2008 +0200
@@ -26,6 +26,10 @@
     import Image
 except ImportError:
     Image = None
+try:
+    import IptcImagePlugin
+except ImportError:
+    IptcImagePlugin = None
 
 try:
     import ExifTags
@@ -111,12 +115,29 @@
                         exif_data['DateTimeOriginal'] = t
                 except (IOError, AttributeError):
                     exif_data = {}
-                exif_cache.update(exif_data)
-                self.__exif = exif_data
+                try:
+                    iptc_data = {}
+                    if self.image is not None and IptcImagePlugin: # we have PIL and the IPTC plugin
+                        iptc = IptcImagePlugin.getiptcinfo(self.image) or {}
+                        for name, key in [
+                            ('headline', (2, 105)),
+                            ('caption', (2, 120)),
+                            ('copyright', (2, 116)),
+                            ('keywords', (2, 25)), ]:
+                            try:
+                                iptc_data[name] = iptc[key]
+                            except KeyError:
+                                pass
+                except:
+                    iptc_data = {}
+                cache_data = (exif_data, iptc_data)
+                exif_cache.update(cache_data)
+                self.__exif, self.__iptc = cache_data
             else:
-                self.__exif = exif_cache.content()
-        return self.__exif
-    exif = property(_get_exif_data) # dict of preprocessed EXIF data (string -> value)
+                self.__exif, self.__iptc = exif_cache.content()
+        return self.__exif, self.__iptc
+    exif = property(lambda self: self._get_exif_data()[0]) # dict of preprocessed EXIF data (string -> value)
+    iptc = property(lambda self: self._get_exif_data()[1]) # dict of preprocessed IPTC data (string -> value)
 
     def _get_ctime(self):
         """ return creation time of image (either from EXIF or file date) as UNIX timestamp """
@@ -215,5 +236,5 @@
             ' ' +
             fmt.text(img.description) +
             '---' +
-            fmt.text(repr(img.exif)))
+            fmt.text(repr(img.iptc)))