def save_image(image, format='JPEG', **options):
    destination = StringIO()
    if format == 'JPEG':
        options.setdefault('quality', 85)
        try:
            image.save(destination, format=format, optimize=1, **options)
        except IOError:
            # Try again, without optimization (PIL can't optimize an image
            # larger than ImageFile.MAXBLOCK, which is 64k by default)
            pass
    image.save(destination, format=format, **options)
    if hasattr(destination, 'seek'):
        destination.seek(0)
    # FIXME: Copying to memory back and forth just because
    # storage expects an object which implements a `chunks` method
    return ContentFile(destination.read())
Save a PIL image to memory and return a StringIO instance.
dataset/ETHPy150Open hcarvalhoalves/django-rest-thumbnails/restthumbnails/processors.py/save_image