<s> from PIL import Image <EOL> class Resizer ( object ) : <EOL> def __init__ ( self , expand = False , filter = Image . ANTIALIAS ) : <EOL> self . expand = expand <EOL> self . filter = filter <EOL> def transformations ( self , size , target_size ) : <EOL> raise NotImplementedError <EOL> def transform ( self , img , transformation , params ) : <EOL> if transformation == '<STR_LIT>' : <EOL> return img . crop ( params ) <EOL> elif transformation == '<STR_LIT>' : <EOL> return img . resize ( params , self . filter ) <EOL> else : <EOL> raise NotImplementedError ( transformation ) <EOL> def get_target_size ( self , size , target_size ) : <EOL> transforms = self . transformations ( size , target_size ) <EOL> for transformation , params in transforms : <EOL> if transformation == '<STR_LIT>' : <EOL> size = params <EOL> elif transformation == '<STR_LIT>' : <EOL> size = ( params [ <NUM_LIT:2> ] - params [ <NUM_LIT:0> ] , params [ <NUM_LIT:3> ] - params [ <NUM_LIT:1> ] ) <EOL> else : <EOL> raise NotImplementedError ( transformation ) <EOL> return size <EOL> def __call__ ( self , img , target_size ) : <EOL> transforms = self . transformations ( img . size , target_size ) <EOL> for transformation , params in transforms : <EOL> img = self . transform ( img , transformation , params ) <EOL> return img <EOL> class ResizeFit ( Resizer ) : <EOL> '''<STR_LIT>''' <EOL> def transformations ( self , size , target_size ) : <EOL> sw , sh = size <EOL> tw , th = target_size <EOL> if not self . expand and sw <= tw and sh <= th : <EOL> return [ ] <EOL> if sw * th > sh * tw : <EOL> h = sh * tw // sw <EOL> return [ ( '<STR_LIT>' , ( tw , h ) ) ] <EOL> else : <EOL> w = sw * th // sh <EOL> return [ ( '<STR_LIT>' , ( w , th ) ) ] <EOL> class ResizeCrop ( Resizer ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . force = kwargs . pop ( '<STR_LIT>' , False ) <EOL> Resizer . __init__ ( self , * args , ** kwargs ) <EOL> assert not ( self . force and self . expand ) <EOL> def transformations ( self , size , target_size ) : <EOL> sw , sh = size <EOL> tw , th = target_size <EOL> if not self . expand and not self . force and sw <= tw and sh <= th : <EOL> return [ ] <EOL> if self . force and ( sw <= tw or sh <= th ) : <EOL> if sw * th > sh * tw : <EOL> tw , th = sh * tw // th , sh <EOL> else : <EOL> tw , th = sw , sw * th // tw <EOL> transforms = [ ] <EOL> if sw * th > sh * tw : <EOL> if sh != th and ( sh > th or self . expand ) : <EOL> w = sw * th // sh <EOL> transforms . append ( ( '<STR_LIT>' , ( w , th ) ) ) <EOL> sw , sh = w , th <EOL> if sw > tw : <EOL> wd = ( sw - tw ) // <NUM_LIT:2> <EOL> transforms . append ( ( '<STR_LIT>' , ( wd , <NUM_LIT:0> , tw + wd , sh ) ) ) <EOL> else : <EOL> if sw != tw and ( sw > tw or self . expand ) : <EOL> h = sh * tw // sw <EOL> transforms . append ( ( '<STR_LIT>' , ( tw , h ) ) ) <EOL> sw , sh = tw , h <EOL> if sh > th : <EOL> hd = ( sh - th ) // <NUM_LIT:2> <EOL> transforms . append ( ( '<STR_LIT>' , ( <NUM_LIT:0> , hd , sw , th + hd ) ) ) <EOL> return transforms <EOL> class ResizeMixed ( Resizer ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , hor_resize , vert_resize , rate = <NUM_LIT:1> ) : <EOL> self . hor_resize = hor_resize <EOL> self . vert_resize = vert_resize <EOL> self . rate = rate <EOL> def get_resizer ( self , size , target_size ) : <EOL> sw , sh = size <EOL> if sw >= sh * self . rate : <EOL> return self . hor_resize <EOL> else : <EOL> return self . vert_resize <EOL> def transformations ( self , size , target_size ) : <EOL> return self . get_resizer ( size , target_size ) . transformations ( size , target_size ) <EOL> def __call__ ( self , img , target_size ) : <EOL> return self . get_resizer ( img . size , target_size ) ( img , target_size ) <EOL> class ResizeFixedWidth ( Resizer ) : <EOL> '''<STR_LIT>''' <EOL> def transformations ( self , size , target_size ) : <EOL> sw , sh = size <EOL> tw , th = target_size <EOL> if not self . expand and sw <= tw : <EOL> return [ ] <EOL> h = sh * tw // sw <EOL> return [ ( '<STR_LIT>' , ( tw , h ) ) ] <EOL> class ResizeFixedHeight ( Resizer ) : <EOL> '''<STR_LIT>''' <EOL> def transformations ( self , size , target_size ) : <EOL> sw , sh = size <EOL> tw , th = target_size <EOL> if not self . expand and sh <= th : <EOL> return [ ] <EOL> w = sw * th // sh <EOL> return [ ( '<STR_LIT>' , ( w , th ) ) ] </s>
