def format_image(self, image, image_format, **kwargs):
        """Returns an image in the request format"""

        if image_format in ('png', 'jpg', 'jpeg', 'gif', 'bmp', 'webp'):
            if image_format != 'webp' and FORCE_WEBP:
                # Always return WebP when supported by the browser
                accept = self.request.META['HTTP_ACCEPT'].split(',')
                if 'image/webp' in accept:
                    image = image.convert('RGBA')
                    image_format = 'webp'
                    kwargs = {'lossless': True}

            if image_format == 'png':
                kwargs['optimize'] = True
            elif image_format == 'jpg':
                image.convert('RGB')
                kwargs['progressive'] = True

            buffer = six.BytesIO()
            image.save(buffer, image_format, **kwargs)
            return buffer.getvalue(), "image/{}".format(image_format)
        else:
            raise ValueError('Unsupported format: {}'.format(image_format))
def format_image ( self , image , image_format , ** kwargs ) : if image_format in ( 'png' , 'jpg' , 'jpeg' , 'gif' , 'bmp' , 'webp' ) : if image_format != 'webp' and FORCE_WEBP : accept = self . request . META [ 'HTTP_ACCEPT' ] . split ( ',' ) if 'image/webp' in accept : image = image . convert ( 'RGBA' ) image_format = 'webp' kwargs = { 'lossless' : True } if image_format == 'png' : kwargs [ 'optimize' ] = True elif image_format == 'jpg' : image . convert ( 'RGB' ) kwargs [ 'progressive' ] = True buffer = six . BytesIO ( ) image . save ( buffer , image_format , ** kwargs ) return buffer . getvalue ( ) , "image/{}" . format ( image_format ) else : raise ValueError ( 'Unsupported format: {}' . format ( image_format ) )
Returns an image in the request format