<s> from __future__ import division <EOL> from sorl . thumbnail . conf import settings <EOL> from sorl . thumbnail . helpers import toint <EOL> from sorl . thumbnail . parsers import parse_crop <EOL> from sorl . thumbnail . parsers import parse_cropbox <EOL> class EngineBase ( object ) : <EOL> """<STR_LIT>""" <EOL> def create ( self , image , geometry , options ) : <EOL> """<STR_LIT>""" <EOL> image = self . cropbox ( image , geometry , options ) <EOL> image = self . orientation ( image , geometry , options ) <EOL> image = self . colorspace ( image , geometry , options ) <EOL> image = self . remove_border ( image , options ) <EOL> image = self . scale ( image , geometry , options ) <EOL> image = self . crop ( image , geometry , options ) <EOL> image = self . rounded ( image , geometry , options ) <EOL> image = self . blur ( image , geometry , options ) <EOL> image = self . padding ( image , geometry , options ) <EOL> return image <EOL> def cropbox ( self , image , geometry , options ) : <EOL> """<STR_LIT>""" <EOL> cropbox = options [ '<STR_LIT>' ] <EOL> if not cropbox : <EOL> return image <EOL> x , y , x2 , y2 = parse_cropbox ( cropbox ) <EOL> return self . _cropbox ( image , x , y , x2 , y2 ) <EOL> def orientation ( self , image , geometry , options ) : <EOL> """<STR_LIT>""" <EOL> if options . get ( '<STR_LIT>' , settings . THUMBNAIL_ORIENTATION ) : <EOL> return self . _orientation ( image ) <EOL> return image <EOL> def colorspace ( self , image , geometry , options ) : <EOL> """<STR_LIT>""" <EOL> colorspace = options [ '<STR_LIT>' ] <EOL> return self . _colorspace ( image , colorspace ) <EOL> def remove_border ( self , image , options ) : <EOL> if options . get ( '<STR_LIT>' , False ) : <EOL> x_image , y_image = self . get_image_size ( image ) <EOL> image = self . _remove_border ( image , x_image , y_image ) <EOL> return image <EOL> def _calculate_scaling_factor ( self , x_image , y_image , geometry , options ) : <EOL> crop = options [ '<STR_LIT>' ] <EOL> factors = ( geometry [ <NUM_LIT:0> ] / x_image , geometry [ <NUM_LIT:1> ] / y_image ) <EOL> return max ( factors ) if crop else min ( factors ) <EOL> def scale ( self , image , geometry , options ) : <EOL> """<STR_LIT>""" <EOL> upscale = options [ '<STR_LIT>' ] <EOL> x_image , y_image = map ( float , self . get_image_size ( image ) ) <EOL> factor = self . _calculate_scaling_factor ( x_image , y_image , geometry , options ) <EOL> if factor < <NUM_LIT:1> or upscale : <EOL> width = toint ( x_image * factor ) <EOL> height = toint ( y_image * factor ) <EOL> image = self . _scale ( image , width , height ) <EOL> return image <EOL> def crop ( self , image , geometry , options ) : <EOL> """<STR_LIT>""" <EOL> crop = options [ '<STR_LIT>' ] <EOL> x_image , y_image = self . get_image_size ( image ) <EOL> if not crop or crop == '<STR_LIT>' : <EOL> return image <EOL> elif crop == '<STR_LIT>' : <EOL> return self . _entropy_crop ( image , geometry [ <NUM_LIT:0> ] , geometry [ <NUM_LIT:1> ] , x_image , y_image ) <EOL> geometry = ( min ( x_image , geometry [ <NUM_LIT:0> ] ) , min ( y_image , geometry [ <NUM_LIT:1> ] ) ) <EOL> x_offset , y_offset = parse_crop ( crop , ( x_image , y_image ) , geometry ) <EOL> return self . _crop ( image , geometry [ <NUM_LIT:0> ] , geometry [ <NUM_LIT:1> ] , x_offset , y_offset ) <EOL> def rounded ( self , image , geometry , options ) : <EOL> """<STR_LIT>""" <EOL> r = options [ '<STR_LIT>' ] <EOL> if not r : <EOL> return image <EOL> return self . _rounded ( image , int ( r ) ) <EOL> def blur ( self , image , geometry , options ) : <EOL> """<STR_LIT>""" <EOL> if options . get ( '<STR_LIT>' ) : <EOL> return self . _blur ( image , int ( options . get ( '<STR_LIT>' ) ) ) <EOL> return image <EOL> def padding ( self , image , geometry , options ) : <EOL> """<STR_LIT>""" <EOL> if options . get ( '<STR_LIT>' ) and self . get_image_size ( image ) != geometry : <EOL> return self . _padding ( image , geometry , options ) <EOL> return image <EOL> def write ( self , image , options , thumbnail ) : <EOL> """<STR_LIT>""" <EOL> format_ = options [ '<STR_LIT>' ] <EOL> quality = options [ '<STR_LIT>' ] <EOL> image_info = options . get ( '<STR_LIT>' , { } ) <EOL> progressive = options . get ( '<STR_LIT>' , settings . THUMBNAIL_PROGRESSIVE ) <EOL> raw_data = self . _get_raw_data ( <EOL> image , format_ , quality , <EOL> image_info = image_info , <EOL> progressive = progressive <EOL> ) <EOL> thumbnail . write ( raw_data ) <EOL> def cleanup ( self , image ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def get_image_ratio ( self , image , options ) : <EOL> """<STR_LIT>""" <EOL> cropbox = options [ '<STR_LIT>' ] <EOL> if cropbox : <EOL> x , y , x2 , y2 = parse_cropbox ( cropbox ) <EOL> x = x2 - x <EOL> y = y2 - y <EOL> else : <EOL> x , y = self . get_image_size ( image ) <EOL> return float ( x ) / y <EOL> def get_image_info ( self , image ) : <EOL> """<STR_LIT>""" <EOL> return { } <EOL> def get_image ( self , source ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def get_image_size ( self , image ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def is_valid_image ( self , raw_data ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def _orientation ( self , image ) : <EOL> """<STR_LIT>""" <EOL> return image <EOL> def _colorspace ( self , image , colorspace ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def _remove_border ( self , image , image_width , image_height ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def _entropy_crop ( self , image , geometry_width , geometry_height , image_width , image_height ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def _scale ( self , image , width , height ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def _crop ( self , image , width , height , x_offset , y_offset ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def _get_raw_data ( self , image , format_ , quality , image_info = None , progressive = False ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def _padding ( self , image , geometry , options ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def _cropbox ( self , image , x , y , x2 , y2 ) : <EOL> raise NotImplementedError ( ) <EOL> def _rounded ( self , image , r ) : <EOL> raise NotImplementedError ( ) <EOL> def _blur ( self , image , radius ) : <EOL> raise NotImplementedError ( ) </s>
