def _do_generate(self):
        if isinstance(self.source, Image.Image):
            data = self.source
        else:
            try:
                if not hasattr(self.source, 'readline'):
                    if not hasattr(self.source, 'read'):
                        source = force_unicode(self.source)
                        if not default_storage.exists(source):
                            raise ThumbnailException('Source does not exist: %s'
                                                     % self.source)
                        file = default_storage.open(source, 'rb')
                        content = ContentFile(file.read())
                        file.close()
                    else:
                        content = ContentFile(self.source.read())
                else:
                    content = ContentFile(self.source.read())
                data = Image.open(content)
            except IOError, detail:
                raise ThumbnailException('%s: %s' % (detail, self.source))
            except MemoryError:
                raise ThumbnailException('Memory Error: %s' % self.source)

        filelike = hasattr(self.dest, 'write')
        if not filelike:
            dest = StringIO()
        else:
            dest = self.dest

        data = self.processor.generate_thumbnail(data, self.width, self.height)

        filename = force_unicode(self.dest)
        try:
            data.save(dest, optimize=1, **self.processor.get_save_options(filename, data))
        except IOError:
            # Try again, without optimization (PIL can't optimize an image
            # larger than ImageFile.MAXBLOCK, which is 64k by default)
            try:
                data.save(dest, **self.processor.get_save_options(filename, data))
            except __HOLE__, e:
                raise ThumbnailException(e)

        if hasattr(self.source, 'seek'):
            self.source.seek(0)
        if filelike:
            dest.seek(0)
        else:
            if default_storage.exists(filename):
                default_storage.delete(filename)
            default_storage.save(filename, ContentFile(dest.getvalue()))
            dest.close()
dataset/ETHPy150Open kylemacfarlane/django-cb-thumbnail/src/cuddlybuddly/thumbnail/main.py/Thumbnail._do_generate