def save_image(image, format='JPEG', **options):
    """
    Save a PIL image to memory and return a StringIO instance.
    """
    destination = StringIO()
    if format == 'JPEG':
        options.setdefault('quality', 85)
        try:
            image.save(destination, format=format, optimize=1, **options)
        except __HOLE__:
            # 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())
dataset/ETHPy150Open hcarvalhoalves/django-rest-thumbnails/restthumbnails/processors.py/save_image