Fix import of JpegImagePlugin
[joel/kofoto.git] / src / packages / kofoto / exifthumbsupport.py
1 """Read-only support for files with embedded JPEG thumbnail in EXIF header."""
2
3 import Image
4 from PIL import JpegImagePlugin
5 from kofoto import EXIF
6 from cStringIO import StringIO
7
8 class ExifThumbImageFile(JpegImagePlugin.JpegImageFile):
9     format = "EXIFTHUMB"
10     format_description = "EXIF JPEG thumbnail"
11
12     def _open(self):
13         try:
14             tags = EXIF.process_file(self.fp, details=False)
15         except: # Work-around for buggy EXIF library.
16             raise SyntaxError("not an EXIFTHUMB file")
17
18         if "JPEGThumbnail" in tags:
19             self.fp = StringIO(tags["JPEGThumbnail"])
20         else:
21             raise SyntaxError("not an EXIFTHUMB file")
22
23         JpegImagePlugin.JpegImageFile._open(self)
24
25 Image.register_open("EXIFTHUMB", ExifThumbImageFile)
26
27 # Known extensions with embedded thumbnails:
28 Image.register_extension("CR2", ".cr2")