def aspect_scale(img, rescale_tuple):
    """Get scaled image dimensions while retaining aspect ratio."""
    bx, by = rescale_tuple
    ix,iy = img.get_size()

    if ix > iy:
        scale_factor = bx/float(ix)
        sy = scale_factor * iy
        if sy > by:
            scale_factor = by/float(iy)
            sx = scale_factor * ix
            sy = by
        else:
            sx = bx
    else:
        scale_factor = by/float(iy)
        sx = scale_factor * ix
        if sx > bx:
            scale_factor = bx/float(ix)
            sx = bx
            sy = scale_factor * iy
        else:
            sy = by

    return int(sx), int(sy)