#NEWLINE# def read( self , extension = None , quality = None ) : #NEWLINE# #INDENT# img_buffer = BytesIO ( ) #NEWLINE# ext = ( extension or self . extension or self . get_default_extension ( ) ) #NEWLINE# options = { #NEWLINE# #INDENT# 'quality' : quality , #NEWLINE# #UNINDENT# } #NEWLINE# if ( ( ext == '.jpg' ) or ( ext == '.jpeg' ) ) : #NEWLINE# #INDENT# options [ 'optimize' ] = True #NEWLINE# if self . context . config . PROGRESSIVE_JPEG : #NEWLINE# #INDENT# options [ 'progressive' ] = True #NEWLINE# #UNINDENT# if ( self . image . mode != 'RGB' ) : #NEWLINE# #INDENT# self . image = self . image . convert ( 'RGB' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# subsampling_config = self . context . config . PILLOW_JPEG_SUBSAMPLING #NEWLINE# qtables_config = self . context . config . PILLOW_JPEG_QTABLES #NEWLINE# if ( ( subsampling_config is not None ) or ( qtables_config is not None ) ) : #NEWLINE# #INDENT# options [ 'quality' ] = 0 #NEWLINE# orig_subsampling = self . subsampling #NEWLINE# orig_qtables = self . qtables #NEWLINE# if ( ( ( subsampling_config == 'keep' ) or ( subsampling_config is None ) ) and ( orig_subsampling is not None ) ) : #NEWLINE# #INDENT# options [ 'subsampling' ] = orig_subsampling #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# options [ 'subsampling' ] = subsampling_config #NEWLINE# #UNINDENT# if ( ( ( qtables_config == 'keep' ) or ( qtables_config is None ) ) and ( orig_qtables and ( 2 <= len ( orig_qtables ) <= 4 ) ) ) : #NEWLINE# #INDENT# options [ 'qtables' ] = orig_qtables #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# options [ 'qtables' ] = qtables_config #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# if ( options [ 'quality' ] is None ) : #NEWLINE# #INDENT# options [ 'quality' ] = self . context . config . QUALITY #NEWLINE# #UNINDENT# if ( self . icc_profile is not None ) : #NEWLINE# #INDENT# options [ 'icc_profile' ] = self . icc_profile #NEWLINE# #UNINDENT# if self . context . config . PRESERVE_EXIF_INFO : #NEWLINE# #INDENT# if ( self . exif is not None ) : #NEWLINE# #INDENT# options [ 'exif' ] = self . exif #NEWLINE# #UNINDENT# #UNINDENT# if ( ( self . image . mode == 'P' ) and self . transparency ) : #NEWLINE# #INDENT# options [ 'transparency' ] = self . transparency #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# if ( ext == '.webp' ) : #NEWLINE# #INDENT# if ( self . image . mode not in [ 'RGB' , 'RGBA' ] ) : #NEWLINE# #INDENT# mode = None #NEWLINE# if ( self . image . mode == 'P' ) : #NEWLINE# #INDENT# mode = 'RGBA' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# mode = ( 'RGBA' if ( self . image . mode [ ( - 1 ) ] == 'A' ) else 'RGB' ) #NEWLINE# #UNINDENT# self . image = self . image . convert ( mode ) #NEWLINE# #UNINDENT# #UNINDENT# if ( ( ext in [ '.png' , '.gif' ] ) and ( self . image . mode == 'CMYK' ) ) : #NEWLINE# #INDENT# self . image = self . image . convert ( 'RGBA' ) #NEWLINE# #UNINDENT# self . image . format = FORMATS [ ext ] #NEWLINE# self . image . save ( img_buffer , FORMATS [ ext ] , ** options ) #NEWLINE# #UNINDENT# except IOError : #NEWLINE# #INDENT# logger . exception ( 'Could not save as improved image, consider to increase ImageFile.MAXBLOCK' ) #NEWLINE# self . image . save ( img_buffer , FORMATS [ ext ] ) #NEWLINE# #UNINDENT# except KeyError : #NEWLINE# #INDENT# logger . exception ( ( 'Image format not found in PIL: %s' % ext ) ) #NEWLINE# ext = self . get_default_extension ( ) #NEWLINE# self . image . format = FORMATS [ ext ] #NEWLINE# self . image . save ( img_buffer , FORMATS [ ext ] ) #NEWLINE# #UNINDENT# results = img_buffer . getvalue ( ) #NEWLINE# img_buffer . close ( ) #NEWLINE# return results