Let PIL recognize/read files with embedded JPEG thumbnails in EXIF information
authorJoel Rosdahl <joel@rosdahl.net>
Wed, 25 Jul 2007 15:04:12 +0000 (17:04 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 25 Jul 2007 15:04:12 +0000 (17:04 +0200)
src/packages/kofoto/exifthumbsupport.py [new file with mode: 0644]
src/packages/kofoto/shelf.py

diff --git a/src/packages/kofoto/exifthumbsupport.py b/src/packages/kofoto/exifthumbsupport.py
new file mode 100644 (file)
index 0000000..d135fb5
--- /dev/null
@@ -0,0 +1,28 @@
+"""Read-only support for files with embedded JPEG thumbnail in EXIF header."""
+
+import Image
+import JpegImagePlugin
+from kofoto import EXIF
+from cStringIO import StringIO
+
+class ExifThumbImageFile(JpegImagePlugin.JpegImageFile):
+    format = "EXIFTHUMB"
+    format_description = "EXIF JPEG thumbnail"
+
+    def _open(self):
+        try:
+            tags = EXIF.process_file(self.fp, details=False)
+        except: # Work-around for buggy EXIF library.
+            raise SyntaxError("not an EXIFTHUMB file")
+
+        if "JPEGThumbnail" in tags:
+            self.fp = StringIO(tags["JPEGThumbnail"])
+        else:
+            raise SyntaxError("not an EXIFTHUMB file")
+
+        JpegImagePlugin.JpegImageFile._open(self)
+
+Image.register_open("EXIFTHUMB", ExifThumbImageFile)
+
+# Known extensions with embedded thumbnails:
+Image.register_extension("CR2", ".cr2")
index 6ad70f3..2fd0852 100644 (file)
@@ -23,6 +23,7 @@ from kofoto.dag import DAG, LoopError
 from kofoto.cachedobject import CachedObject
 from kofoto.albumtype import AlbumType
 from kofoto.imageversiontype import ImageVersionType
+import kofoto.exifthumbsupport
 import kofoto.shelfupgrade
 import kofoto.shelfschema
 from kofoto.shelfexceptions import \