Rescale the image to the new geometry.

def set_geometry(im, width_height):
    """Rescale the image to the new geometry.
    """
    width, height = width_height
    if not width and not height:
        return im

    im_width, im_height = im.size

    # Geometry match the current size?
    if (width is None) or (im_width == width):
        if (height is None) or (im_height == height):
            return im

    ratio = float(im_width) / im_height

    if width and height:
        new_width = width
        new_height = int(ceil(width / ratio))
        if new_height < height:
            new_height = height
            new_width = int(ceil(height * ratio))
    elif height:
        new_width = int(ceil(height * ratio))
        new_height = height
    else:
        new_width = width
        new_height = int(ceil(width / ratio))

    im.resize(new_width, new_height)
    box = get_box(new_width, new_height, width, height)
    im.crop(*box, reset_coords=True)

    return im