jbaiter / jpegtran-cffi

Fast, (mostly) lossless JPEG transformations with Python
http://jpegtran-cffi.readthedocs.org
MIT License
145 stars 23 forks source link

manipulating an image w/o exif thumbnail throws error (includes workaround) #24

Open benfry opened 5 years ago

benfry commented 5 years ago

Thanks for your work on this library.

I ran into a problem when trying to use it with images that had no EXIF thumbnail—instead of lib.Exif(self.data).thumbnail being set to None, it's a zero-length byte array. So when the exif_thumbnail() function runs, it errors out because constructing JPEGImage(blob=None) is gonna throw an error.

A workaround is below (or here, though I suspect this is just papering over an upstream problem in a function where None should be returned, and instead it's getting wrapped as a byte array.

    @property
    def exif_thumbnail(self):
        """ EXIF thumbnail.

        :return:  EXIF thumbnail in JPEG format
        :rtype:   str

        """
        try:
            thumb = lib.Exif(self.data).thumbnail
            if len(thumb) == 0:
                return None
            return JPEGImage(blob=thumb)
        except lib.ExifException:
            return None