<s> import Image <EOL> import operator <EOL> def _border ( border ) : <EOL> if type ( border ) is type ( ( ) ) : <EOL> if len ( border ) == <NUM_LIT:2> : <EOL> left , top = right , bottom = border <EOL> elif len ( border ) == <NUM_LIT:4> : <EOL> left , top , right , bottom = border <EOL> else : <EOL> left = top = right = bottom = border <EOL> return left , top , right , bottom <EOL> def _color ( color , mode ) : <EOL> if Image . isStringType ( color ) : <EOL> import ImageColor <EOL> color = ImageColor . getcolor ( color , mode ) <EOL> return color <EOL> def _lut ( image , lut ) : <EOL> if image . mode == "<STR_LIT:P>" : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> elif image . mode in ( "<STR_LIT:L>" , "<STR_LIT>" ) : <EOL> if image . mode == "<STR_LIT>" and len ( lut ) == <NUM_LIT> : <EOL> lut = lut + lut + lut <EOL> return image . point ( lut ) <EOL> else : <EOL> raise IOError , "<STR_LIT>" <EOL> def autocontrast ( image , cutoff = <NUM_LIT:0> , ignore = None ) : <EOL> "<STR_LIT>" <EOL> histogram = image . histogram ( ) <EOL> lut = [ ] <EOL> for layer in range ( <NUM_LIT:0> , len ( histogram ) , <NUM_LIT> ) : <EOL> h = histogram [ layer : layer + <NUM_LIT> ] <EOL> if ignore is not None : <EOL> try : <EOL> h [ ignore ] = <NUM_LIT:0> <EOL> except TypeError : <EOL> for ix in ignore : <EOL> h [ ix ] = <NUM_LIT:0> <EOL> if cutoff : <EOL> n = <NUM_LIT:0> <EOL> for ix in range ( <NUM_LIT> ) : <EOL> n = n + h [ ix ] <EOL> cut = n * cutoff / <NUM_LIT:100> <EOL> for lo in range ( <NUM_LIT> ) : <EOL> if cut > h [ lo ] : <EOL> cut = cut - h [ lo ] <EOL> h [ lo ] = <NUM_LIT:0> <EOL> else : <EOL> h [ lo ] = h [ lo ] - cut <EOL> cut = <NUM_LIT:0> <EOL> if cut <= <NUM_LIT:0> : <EOL> break <EOL> cut = n * cutoff / <NUM_LIT:100> <EOL> for hi in range ( <NUM_LIT:255> , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> if cut > h [ hi ] : <EOL> cut = cut - h [ hi ] <EOL> h [ hi ] = <NUM_LIT:0> <EOL> else : <EOL> h [ hi ] = h [ hi ] - cut <EOL> cut = <NUM_LIT:0> <EOL> if cut <= <NUM_LIT:0> : <EOL> break <EOL> for lo in range ( <NUM_LIT> ) : <EOL> if h [ lo ] : <EOL> break <EOL> for hi in range ( <NUM_LIT:255> , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> if h [ hi ] : <EOL> break <EOL> if hi <= lo : <EOL> lut . extend ( range ( <NUM_LIT> ) ) <EOL> else : <EOL> scale = <NUM_LIT> / ( hi - lo ) <EOL> offset = - lo * scale <EOL> for ix in range ( <NUM_LIT> ) : <EOL> ix = int ( ix * scale + offset ) <EOL> if ix < <NUM_LIT:0> : <EOL> ix = <NUM_LIT:0> <EOL> elif ix > <NUM_LIT:255> : <EOL> ix = <NUM_LIT:255> <EOL> lut . append ( ix ) <EOL> return _lut ( image , lut ) <EOL> def colorize ( image , black , white ) : <EOL> "<STR_LIT>" <EOL> assert image . mode == "<STR_LIT:L>" <EOL> black = _color ( black , "<STR_LIT>" ) <EOL> white = _color ( white , "<STR_LIT>" ) <EOL> red = [ ] ; green = [ ] ; blue = [ ] <EOL> for i in range ( <NUM_LIT> ) : <EOL> red . append ( black [ <NUM_LIT:0> ] + i * ( white [ <NUM_LIT:0> ] - black [ <NUM_LIT:0> ] ) / <NUM_LIT:255> ) <EOL> green . append ( black [ <NUM_LIT:1> ] + i * ( white [ <NUM_LIT:1> ] - black [ <NUM_LIT:1> ] ) / <NUM_LIT:255> ) <EOL> blue . append ( black [ <NUM_LIT:2> ] + i * ( white [ <NUM_LIT:2> ] - black [ <NUM_LIT:2> ] ) / <NUM_LIT:255> ) <EOL> image = image . convert ( "<STR_LIT>" ) <EOL> return _lut ( image , red + green + blue ) <EOL> def crop ( image , border = <NUM_LIT:0> ) : <EOL> "<STR_LIT>" <EOL> left , top , right , bottom = _border ( border ) <EOL> return image . crop ( <EOL> ( left , top , image . size [ <NUM_LIT:0> ] - right , image . size [ <NUM_LIT:1> ] - bottom ) <EOL> ) <EOL> def deform ( image , deformer , resample = Image . BILINEAR ) : <EOL> "<STR_LIT>" <EOL> return image . transform ( <EOL> image . size , Image . MESH , deformer . getmesh ( image ) , resample <EOL> ) <EOL> def equalize ( image , mask = None ) : <EOL> "<STR_LIT>" <EOL> if image . mode == "<STR_LIT:P>" : <EOL> image = image . convert ( "<STR_LIT>" ) <EOL> h = image . histogram ( mask ) <EOL> lut = [ ] <EOL> for b in range ( <NUM_LIT:0> , len ( h ) , <NUM_LIT> ) : <EOL> histo = filter ( None , h [ b : b + <NUM_LIT> ] ) <EOL> if len ( histo ) <= <NUM_LIT:1> : <EOL> lut . extend ( range ( <NUM_LIT> ) ) <EOL> else : <EOL> step = ( reduce ( operator . add , histo ) - histo [ - <NUM_LIT:1> ] ) / <NUM_LIT:255> <EOL> if not step : <EOL> lut . extend ( range ( <NUM_LIT> ) ) <EOL> else : <EOL> n = step / <NUM_LIT:2> <EOL> for i in range ( <NUM_LIT> ) : <EOL> lut . append ( n / step ) <EOL> n = n + h [ i + b ] <EOL> return _lut ( image , lut ) <EOL> def expand ( image , border = <NUM_LIT:0> , fill = <NUM_LIT:0> ) : <EOL> "<STR_LIT>" <EOL> left , top , right , bottom = _border ( border ) <EOL> width = left + image . size [ <NUM_LIT:0> ] + right <EOL> height = top + image . size [ <NUM_LIT:1> ] + bottom <EOL> out = Image . new ( image . mode , ( width , height ) , _color ( fill , image . mode ) ) <EOL> out . paste ( image , ( left , top ) ) <EOL> return out <EOL> def fit ( image , size , method = Image . NEAREST , bleed = <NUM_LIT:0.0> , centering = ( <NUM_LIT:0.5> , <NUM_LIT:0.5> ) ) : <EOL> """<STR_LIT>""" <EOL> if type ( centering ) != type ( [ ] ) : <EOL> centering = [ centering [ <NUM_LIT:0> ] , centering [ <NUM_LIT:1> ] ] <EOL> if centering [ <NUM_LIT:0> ] > <NUM_LIT:1.0> or centering [ <NUM_LIT:0> ] < <NUM_LIT:0.0> : <EOL> centering [ <NUM_LIT:0> ] = <NUM_LIT> <EOL> if centering [ <NUM_LIT:1> ] > <NUM_LIT:1.0> or centering [ <NUM_LIT:1> ] < <NUM_LIT:0.0> : <EOL> centering [ <NUM_LIT:1> ] = <NUM_LIT> <EOL> if bleed > <NUM_LIT> or bleed < <NUM_LIT:0.0> : <EOL> bleed = <NUM_LIT:0.0> <EOL> bleedPixels = ( <EOL> int ( ( float ( bleed ) * float ( image . size [ <NUM_LIT:0> ] ) ) + <NUM_LIT:0.5> ) , <EOL> int ( ( float ( bleed ) * float ( image . size [ <NUM_LIT:1> ] ) ) + <NUM_LIT:0.5> ) <EOL> ) <EOL> liveArea = ( <EOL> bleedPixels [ <NUM_LIT:0> ] , bleedPixels [ <NUM_LIT:1> ] , image . size [ <NUM_LIT:0> ] - bleedPixels [ <NUM_LIT:0> ] - <NUM_LIT:1> , <EOL> image . size [ <NUM_LIT:1> ] - bleedPixels [ <NUM_LIT:1> ] - <NUM_LIT:1> <EOL> ) <EOL> liveSize = ( liveArea [ <NUM_LIT:2> ] - liveArea [ <NUM_LIT:0> ] , liveArea [ <NUM_LIT:3> ] - liveArea [ <NUM_LIT:1> ] ) <EOL> liveAreaAspectRatio = float ( liveSize [ <NUM_LIT:0> ] ) / float ( liveSize [ <NUM_LIT:1> ] ) <EOL> aspectRatio = float ( size [ <NUM_LIT:0> ] ) / float ( size [ <NUM_LIT:1> ] ) <EOL> if liveAreaAspectRatio >= aspectRatio : <EOL> cropWidth = int ( ( aspectRatio * float ( liveSize [ <NUM_LIT:1> ] ) ) + <NUM_LIT:0.5> ) <EOL> cropHeight = liveSize [ <NUM_LIT:1> ] <EOL> else : <EOL> cropWidth = liveSize [ <NUM_LIT:0> ] <EOL> cropHeight = int ( ( float ( liveSize [ <NUM_LIT:0> ] ) / aspectRatio ) + <NUM_LIT:0.5> ) <EOL> leftSide = int ( liveArea [ <NUM_LIT:0> ] + ( float ( liveSize [ <NUM_LIT:0> ] - cropWidth ) * centering [ <NUM_LIT:0> ] ) ) <EOL> if leftSide < <NUM_LIT:0> : <EOL> leftSide = <NUM_LIT:0> <EOL> topSide = int ( liveArea [ <NUM_LIT:1> ] + ( float ( liveSize [ <NUM_LIT:1> ] - cropHeight ) * centering [ <NUM_LIT:1> ] ) ) <EOL> if topSide < <NUM_LIT:0> : <EOL> topSide = <NUM_LIT:0> <EOL> out = image . crop ( <EOL> ( leftSide , topSide , leftSide + cropWidth , topSide + cropHeight ) <EOL> ) <EOL> return out . resize ( size , method ) <EOL> def flip ( image ) : <EOL> "<STR_LIT>" <EOL> return image . transpose ( Image . FLIP_TOP_BOTTOM ) <EOL> def grayscale ( image ) : <EOL> "<STR_LIT>" <EOL> return image . convert ( "<STR_LIT:L>" ) <EOL> def invert ( image ) : <EOL> "<STR_LIT>" <EOL> lut = [ ] <EOL> for i in range ( <NUM_LIT> ) : <EOL> lut . append ( <NUM_LIT:255> - i ) <EOL> return _lut ( image , lut ) <EOL> def mirror ( image ) : <EOL> "<STR_LIT>" <EOL> return image . transpose ( Image . FLIP_LEFT_RIGHT ) <EOL> def posterize ( image , bits ) : <EOL> "<STR_LIT>" <EOL> lut = [ ] <EOL> mask = ~ ( <NUM_LIT:2> ** ( <NUM_LIT:8> - bits ) - <NUM_LIT:1> ) <EOL> for i in range ( <NUM_LIT> ) : <EOL> lut . append ( i & mask ) <EOL> return _lut ( image , lut ) <EOL> def solarize ( image , threshold = <NUM_LIT> ) : <EOL> "<STR_LIT>" <EOL> lut = [ ] <EOL> for i in range ( <NUM_LIT> ) : <EOL> if i < threshold : <EOL> lut . append ( i ) <EOL> else : <EOL> lut . append ( <NUM_LIT:255> - i ) <EOL> return _lut ( image , lut ) <EOL> def gaussian_blur ( im , radius = None ) : <EOL> """<STR_LIT>""" <EOL> if radius is None : <EOL> radius = <NUM_LIT> <EOL> im . load ( ) <EOL> return im . im . gaussian_blur ( radius ) <EOL> gblur = gaussian_blur <EOL> def unsharp_mask ( im , radius = None , percent = None , threshold = None ) : <EOL> """<STR_LIT>""" <EOL> if radius is None : <EOL> radius = <NUM_LIT> <EOL> if percent is None : <EOL> percent = <NUM_LIT> <EOL> if threshold is None : <EOL> threshold = <NUM_LIT:3> <EOL> im . load ( ) <EOL> return im . im . unsharp_mask ( radius , percent , threshold ) <EOL> usm = unsharp_mask </s>
