def get_scaled_cutout_wdht_view(shp, x1, y1, x2, y2, new_wd, new_ht):
    """
    Like get_scaled_cutout_wdht, but returns the view/slice to extract
    from an image instead of the extraction itself.
    """
    x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
    new_wd, new_ht = int(new_wd), int(new_ht)

    # calculate dimensions of NON-scaled cutout
    old_wd = x2 - x1 + 1
    old_ht = y2 - y1 + 1
    max_x, max_y = shp[1] - 1, shp[0] - 1

    if (new_wd != old_wd) or (new_ht != old_ht):
        # Make indexes and scale them
        # Is there a more efficient way to do this?
        yi = np.mgrid[0:new_ht].reshape(-1, 1)
        xi = np.mgrid[0:new_wd].reshape(1, -1)
        iscale_x = float(old_wd) / float(new_wd)
        iscale_y = float(old_ht) / float(new_ht)

        xi = (x1 + xi * iscale_x).clip(0, max_x).astype(np.int, copy=False)
        yi = (y1 + yi * iscale_y).clip(0, max_y).astype(np.int, copy=False)
        wd, ht = xi.shape[1], yi.shape[0]

        # bounds check against shape (to protect future data access)
        xi_max, yi_max = xi[0, -1], yi[-1, 0]
        assert xi_max <= max_x, ValueError("X index (%d) exceeds shape bounds (%d)" % (xi_max, max_x))
        assert yi_max <= max_y, ValueError("Y index (%d) exceeds shape bounds (%d)" % (yi_max, max_y))

        view = np.s_[yi, xi]

    else:
        # simple stepped view will do, because new view is same as old
        wd, ht = old_wd, old_ht
        view = np.s_[y1:y2 + 1, x1:x2 + 1]

    # Calculate actual scale used (vs. desired)
    old_wd, old_ht = max(old_wd, 1), max(old_ht, 1)
    scale_x = float(wd) / old_wd
    scale_y = float(ht) / old_ht

    # return view + actual scale factors used
    return (view, (scale_x, scale_y))
def get_scaled_cutout_wdht_view ( shp , x1 , y1 , x2 , y2 , new_wd , new_ht ) : x1 , y1 , x2 , y2 = int ( x1 ) , int ( y1 ) , int ( x2 ) , int ( y2 ) new_wd , new_ht = int ( new_wd ) , int ( new_ht ) old_wd = x2 - x1 + 1 old_ht = y2 - y1 + 1 max_x , max_y = shp [ 1 ] - 1 , shp [ 0 ] - 1 if ( new_wd != old_wd ) or ( new_ht != old_ht ) : yi = np . mgrid [ 0 : new_ht ] . reshape ( - 1 , 1 ) xi = np . mgrid [ 0 : new_wd ] . reshape ( 1 , - 1 ) iscale_x = float ( old_wd ) / float ( new_wd ) iscale_y = float ( old_ht ) / float ( new_ht ) xi = ( x1 + xi * iscale_x ) . clip ( 0 , max_x ) . astype ( np . int , copy = False ) yi = ( y1 + yi * iscale_y ) . clip ( 0 , max_y ) . astype ( np . int , copy = False ) wd , ht = xi . shape [ 1 ] , yi . shape [ 0 ] xi_max , yi_max = xi [ 0 , - 1 ] , yi [ - 1 , 0 ] assert xi_max <= max_x , ValueError ( "X index (%d) exceeds shape bounds (%d)" % ( xi_max , max_x ) ) assert yi_max <= max_y , ValueError ( "Y index (%d) exceeds shape bounds (%d)" % ( yi_max , max_y ) ) view = np . s_ [ yi , xi ] else : wd , ht = old_wd , old_ht view = np . s_ [ y1 : y2 + 1 , x1 : x2 + 1 ] old_wd , old_ht = max ( old_wd , 1 ) , max ( old_ht , 1 ) scale_x = float ( wd ) / old_wd scale_y = float ( ht ) / old_ht return ( view , ( scale_x , scale_y ) )
Like get_scaled_cutout_wdht, but returns the view/slice to extract
    from an image instead of the extraction itself.