def get_new_dims(h, w, target_h, target_w):
    """Returns new dimensions that respect the old aspect ratio.

    `target_h` and `target_w` are usually the same value, since
    target dimensions are most likely squares.
    """

    if h > w:
        r = target_h / float(h)
        new_h = target_h
        new_w = int(w * r)
    else:
        r = target_w / float(w)
        new_h = int(h * r)
        new_w = target_w
    return new_h, new_w