def imrescale(img, scale, return_scale=False, interpolation='<STR_LIT>'):
h, w = img.shape[:<NUM_LIT:2>]<EOL>if isinstance(scale, (float, int)):<EOL><INDENT>if scale <= <NUM_LIT:0>:<EOL><INDENT>raise ValueError(<EOL>'<STR_LIT>'.format(scale))<EOL><DEDENT>scale_factor = scale<EOL><DEDENT>elif isinstance(scale, tuple):<EOL><INDENT>max_long_edge = max(scale)<EOL>max_short_edge = min(scale)<EOL>scale_factor = min(max_long_edge / max(h, w),<EOL>max_short_edge / min(h, w))<EOL><DEDENT>else:<EOL><INDENT>raise TypeError(<EOL>'<STR_LIT>'.format(<EOL>type(scale)))<EOL><DEDENT>new_size = _scale_size((w, h), scale_factor)<EOL>rescaled_img = imresize(img, new_size, interpolation=interpolation)<EOL>if return_scale:<EOL><INDENT>return rescaled_img, scale_factor<EOL><DEDENT>else:<EOL><INDENT>return rescaled_img<EOL><DEDENT>
Resize image while keeping the aspect ratio.

    Args:
        img (ndarray): The input image.
        scale (float or tuple[int]): The scaling factor or maximum size.
            If it is a float number, then the image will be rescaled by this
            factor, else if it is a tuple of 2 integers, then the image will
            be rescaled as large as possible within the scale.
        return_scale (bool): Whether to return the scaling factor besides the
            rescaled image.
        interpolation (str): Same as :func:`resize`.

    Returns:
        ndarray: The rescaled image.