def read(self, extension=None, quality=None):
    img_buffer = BytesIO()
    requested_extension = (extension or self.extension)
    if (self.context.config.PILLOW_PRESERVE_INDEXED_MODE and (requested_extension in [None, '.png', '.gif']) and (self.original_mode in ['P', '1']) and (self.original_mode != self.image.mode)):
        if (self.original_mode == '1'):
            self.image = self.image.convert('1')
        else:
            quantize_method = (Image.LIBIMAGEQUANT if pillow_features.check('libimagequant') else None)
            self.image = self.image.quantize(method=quantize_method)
    ext = (requested_extension or self.get_default_extension())
    options = {'quality': quality}
    if (ext in ('.jpg', '.jpeg')):
        options['optimize'] = True
        if self.context.config.PROGRESSIVE_JPEG:
            options['progressive'] = True
        if (self.image.mode != 'RGB'):
            self.image = self.image.convert('RGB')
        else:
            subsampling_config = self.context.config.PILLOW_JPEG_SUBSAMPLING
            qtables_config = self.context.config.PILLOW_JPEG_QTABLES
            if ((subsampling_config is not None) or (qtables_config is not None)):
                options['quality'] = 0
                orig_subsampling = self.subsampling
                orig_qtables = self.qtables
                if (((subsampling_config == 'keep') or (subsampling_config is None)) and (orig_subsampling is not None)):
                    options['subsampling'] = orig_subsampling
                else:
                    options['subsampling'] = subsampling_config
                if (((qtables_config == 'keep') or (qtables_config is None)) and (orig_qtables and (2 <= len(orig_qtables) <= 4))):
                    options['qtables'] = orig_qtables
                else:
                    options['qtables'] = qtables_config
    if ((ext == '.png') and (self.context.config.PNG_COMPRESSION_LEVEL is not None)):
        options['compress_level'] = self.context.config.PNG_COMPRESSION_LEVEL
    if (options['quality'] is None):
        options['quality'] = self.context.config.QUALITY
    if (self.icc_profile is not None):
        options['icc_profile'] = self.icc_profile
    if self.context.config.PRESERVE_EXIF_INFO:
        if (self.exif is not None):
            options['exif'] = self.exif
    try:
        if (ext == '.webp'):
            if (options['quality'] == 100):
                logger.debug('webp quality is 100, using lossless instead')
                options['lossless'] = True
                options.pop('quality')
            if (self.image.mode not in ['RGB', 'RGBA']):
                if (self.image.mode == 'P'):
                    mode = 'RGBA'
                else:
                    mode = ('RGBA' if (self.image.mode[(- 1)] == 'A') else 'RGB')
                self.image = self.image.convert(mode)
        if ((ext in ['.png', '.gif']) and (self.image.mode == 'CMYK')):
            self.image = self.image.convert('RGBA')
        self.image.format = FORMATS.get(ext, FORMATS[self.get_default_extension()])
        self.image.save(img_buffer, self.image.format, **options)
    except IOError:
        logger.exception('Could not save as improved image, consider to increase ImageFile.MAXBLOCK')
        self.image.save(img_buffer, FORMATS[ext])
    results = img_buffer.getvalue()
    img_buffer.close()
    self.extension = ext
    return results
def read(self, extension=None, quality=None):
    img_buffer = BytesIO()
    requested_extension = (extension or self.extension)
    if (self.context.config.PILLOW_PRESERVE_INDEXED_MODE and (requested_extension in [None, '.png', '.gif']) and (self.original_mode in ['P', '1']) and (self.original_mode != self.image.mode)):
        if (self.original_mode == '1'):
            self.image = self.image.convert('1')
        else:
            quantize_method = (Image.LIBIMAGEQUANT if pillow_features.check('libimagequant') else None)
            self.image = self.image.quantize(method=quantize_method)
    ext = (requested_extension or self.get_default_extension())
    options = {'quality': quality}
    if (ext in ('.jpg', '.jpeg')):
        options['optimize'] = True
        if self.context.config.PROGRESSIVE_JPEG:
            options['progressive'] = True
        if (self.image.mode != 'RGB'):
            self.image = self.image.convert('RGB')
        else:
            subsampling_config = self.context.config.PILLOW_JPEG_SUBSAMPLING
            qtables_config = self.context.config.PILLOW_JPEG_QTABLES
            if ((subsampling_config is not None) or (qtables_config is not None)):
                options['quality'] = 0
                orig_subsampling = self.subsampling
                orig_qtables = self.qtables
                if (((subsampling_config == 'keep') or (subsampling_config is None)) and (orig_subsampling is not None)):
                    options['subsampling'] = orig_subsampling
                else:
                    options['subsampling'] = subsampling_config
                if (((qtables_config == 'keep') or (qtables_config is None)) and (orig_qtables and (2 <= len(orig_qtables) <= 4))):
                    options['qtables'] = orig_qtables
                else:
                    options['qtables'] = qtables_config
    if ((ext == '.png') and (self.context.config.PNG_COMPRESSION_LEVEL is not None)):
        options['compress_level'] = self.context.config.PNG_COMPRESSION_LEVEL
    if (options['quality'] is None):
        options['quality'] = self.context.config.QUALITY
    if (self.icc_profile is not None):
        options['icc_profile'] = self.icc_profile
    if self.context.config.PRESERVE_EXIF_INFO:
        if (self.exif is not None):
            options['exif'] = self.exif
    try:
        if (ext == '.webp'):
            if (options['quality'] == 100):
                logger.debug('webp quality is 100, using lossless instead')
                options['lossless'] = True
                options.pop('quality')
            if (self.image.mode not in ['RGB', 'RGBA']):
                if (self.image.mode == 'P'):
                    mode = 'RGBA'
                else:
                    mode = ('RGBA' if (self.image.mode[(- 1)] == 'A') else 'RGB')
                self.image = self.image.convert(mode)
        if ((ext in ['.png', '.gif']) and (self.image.mode == 'CMYK')):
            self.image = self.image.convert('RGBA')
        self.image.format = FORMATS.get(ext, FORMATS[self.get_default_extension()])
        self.image.save(img_buffer, self.image.format, **options)
    except IOError:
        logger.exception('Could not save as improved image, consider to increase ImageFile.MAXBLOCK')
        self.image.save(img_buffer, FORMATS[ext])
    results = img_buffer.getvalue()
    img_buffer.close()
    self.extension = ext
    return results