<s> import numpy as np <EOL> from scipy import ndimage as ndi <EOL> from . . measure import block_reduce <EOL> from . _geometric import ( warp , SimilarityTransform , AffineTransform , <EOL> _convert_warp_input , _clip_warp_output , <EOL> _to_ndimage_mode ) <EOL> def resize ( image , output_shape , order = <NUM_LIT:1> , mode = '<STR_LIT>' , cval = <NUM_LIT:0> , clip = True , <EOL> preserve_range = False ) : <EOL> """<STR_LIT>""" <EOL> rows , cols = output_shape [ <NUM_LIT:0> ] , output_shape [ <NUM_LIT:1> ] <EOL> orig_rows , orig_cols = image . shape [ <NUM_LIT:0> ] , image . shape [ <NUM_LIT:1> ] <EOL> row_scale = float ( orig_rows ) / rows <EOL> col_scale = float ( orig_cols ) / cols <EOL> if len ( output_shape ) == <NUM_LIT:3> and ( image . ndim == <NUM_LIT:2> <EOL> or output_shape [ <NUM_LIT:2> ] != image . shape [ <NUM_LIT:2> ] ) : <EOL> ndi_mode = _to_ndimage_mode ( mode ) <EOL> dim = output_shape [ <NUM_LIT:2> ] <EOL> if image . ndim == <NUM_LIT:2> : <EOL> image = image [ : , : , np . newaxis ] <EOL> orig_dim = image . shape [ <NUM_LIT:2> ] <EOL> dim_scale = float ( orig_dim ) / dim <EOL> map_rows , map_cols , map_dims = np . mgrid [ : rows , : cols , : dim ] <EOL> map_rows = row_scale * ( map_rows + <NUM_LIT:0.5> ) - <NUM_LIT:0.5> <EOL> map_cols = col_scale * ( map_cols + <NUM_LIT:0.5> ) - <NUM_LIT:0.5> <EOL> map_dims = dim_scale * ( map_dims + <NUM_LIT:0.5> ) - <NUM_LIT:0.5> <EOL> coord_map = np . array ( [ map_rows , map_cols , map_dims ] ) <EOL> image = _convert_warp_input ( image , preserve_range ) <EOL> out = ndi . map_coordinates ( image , coord_map , order = order , <EOL> mode = ndi_mode , cval = cval ) <EOL> _clip_warp_output ( image , out , order , mode , cval , clip ) <EOL> else : <EOL> if rows == <NUM_LIT:1> and cols == <NUM_LIT:1> : <EOL> tform = AffineTransform ( translation = ( orig_cols / <NUM_LIT> - <NUM_LIT:0.5> , <EOL> orig_rows / <NUM_LIT> - <NUM_LIT:0.5> ) ) <EOL> else : <EOL> src_corners = np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:1> ] , [ <NUM_LIT:1> , rows ] , [ cols , rows ] ] ) - <NUM_LIT:1> <EOL> dst_corners = np . zeros ( src_corners . shape , dtype = np . double ) <EOL> dst_corners [ : , <NUM_LIT:0> ] = col_scale * ( src_corners [ : , <NUM_LIT:0> ] + <NUM_LIT:0.5> ) - <NUM_LIT:0.5> <EOL> dst_corners [ : , <NUM_LIT:1> ] = row_scale * ( src_corners [ : , <NUM_LIT:1> ] + <NUM_LIT:0.5> ) - <NUM_LIT:0.5> <EOL> tform = AffineTransform ( ) <EOL> tform . estimate ( src_corners , dst_corners ) <EOL> out = warp ( image , tform , output_shape = output_shape , order = order , <EOL> mode = mode , cval = cval , clip = clip , <EOL> preserve_range = preserve_range ) <EOL> return out <EOL> def rescale ( image , scale , order = <NUM_LIT:1> , mode = '<STR_LIT>' , cval = <NUM_LIT:0> , clip = True , <EOL> preserve_range = False ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> row_scale , col_scale = scale <EOL> except TypeError : <EOL> row_scale = col_scale = scale <EOL> orig_rows , orig_cols = image . shape [ <NUM_LIT:0> ] , image . shape [ <NUM_LIT:1> ] <EOL> rows = np . round ( row_scale * orig_rows ) <EOL> cols = np . round ( col_scale * orig_cols ) <EOL> output_shape = ( rows , cols ) <EOL> return resize ( image , output_shape , order = order , mode = mode , cval = cval , <EOL> clip = clip , preserve_range = preserve_range ) <EOL> def rotate ( image , angle , resize = False , center = None , order = <NUM_LIT:1> , mode = '<STR_LIT>' , <EOL> cval = <NUM_LIT:0> , clip = True , preserve_range = False ) : <EOL> """<STR_LIT>""" <EOL> rows , cols = image . shape [ <NUM_LIT:0> ] , image . shape [ <NUM_LIT:1> ] <EOL> if center is None : <EOL> center = np . array ( ( cols , rows ) ) / <NUM_LIT> - <NUM_LIT:0.5> <EOL> else : <EOL> center = np . asarray ( center ) <EOL> tform1 = SimilarityTransform ( translation = center ) <EOL> tform2 = SimilarityTransform ( rotation = np . deg2rad ( angle ) ) <EOL> tform3 = SimilarityTransform ( translation = - center ) <EOL> tform = tform3 + tform2 + tform1 <EOL> output_shape = None <EOL> if resize : <EOL> corners = np . array ( [ <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , rows - <NUM_LIT:1> ] , <EOL> [ cols - <NUM_LIT:1> , rows - <NUM_LIT:1> ] , <EOL> [ cols - <NUM_LIT:1> , <NUM_LIT:0> ] <EOL> ] ) <EOL> corners = tform . inverse ( corners ) <EOL> minc = corners [ : , <NUM_LIT:0> ] . min ( ) <EOL> minr = corners [ : , <NUM_LIT:1> ] . min ( ) <EOL> maxc = corners [ : , <NUM_LIT:0> ] . max ( ) <EOL> maxr = corners [ : , <NUM_LIT:1> ] . max ( ) <EOL> out_rows = maxr - minr + <NUM_LIT:1> <EOL> out_cols = maxc - minc + <NUM_LIT:1> <EOL> output_shape = np . ceil ( ( out_rows , out_cols ) ) <EOL> translation = ( minc , minr ) <EOL> tform4 = SimilarityTransform ( translation = translation ) <EOL> tform = tform4 + tform <EOL> return warp ( image , tform , output_shape = output_shape , order = order , <EOL> mode = mode , cval = cval , clip = clip , preserve_range = preserve_range ) <EOL> def downscale_local_mean ( image , factors , cval = <NUM_LIT:0> , clip = True ) : <EOL> """<STR_LIT>""" <EOL> return block_reduce ( image , factors , np . mean , cval ) <EOL> def _swirl_mapping ( xy , center , rotation , strength , radius ) : <EOL> x , y = xy . T <EOL> x0 , y0 = center <EOL> rho = np . sqrt ( ( x - x0 ) ** <NUM_LIT:2> + ( y - y0 ) ** <NUM_LIT:2> ) <EOL> radius = radius / <NUM_LIT:5> * np . log ( <NUM_LIT:2> ) <EOL> theta = rotation + strength * np . exp ( - rho / radius ) + np . arctan2 ( y - y0 , x - x0 ) <EOL> xy [ ... , <NUM_LIT:0> ] = x0 + rho * np . cos ( theta ) <EOL> xy [ ... , <NUM_LIT:1> ] = y0 + rho * np . sin ( theta ) <EOL> return xy <EOL> def swirl ( image , center = None , strength = <NUM_LIT:1> , radius = <NUM_LIT:100> , rotation = <NUM_LIT:0> , <EOL> output_shape = None , order = <NUM_LIT:1> , mode = '<STR_LIT>' , cval = <NUM_LIT:0> , clip = True , <EOL> preserve_range = False ) : <EOL> """<STR_LIT>""" <EOL> if center is None : <EOL> center = np . array ( image . shape ) [ : <NUM_LIT:2> ] / <NUM_LIT:2> <EOL> warp_args = { '<STR_LIT>' : center , <EOL> '<STR_LIT>' : rotation , <EOL> '<STR_LIT>' : strength , <EOL> '<STR_LIT>' : radius } <EOL> return warp ( image , _swirl_mapping , map_args = warp_args , <EOL> output_shape = output_shape , order = order , mode = mode , cval = cval , <EOL> clip = clip , preserve_range = preserve_range ) </s>
