def crop_to_ratio(im, desired_ratio=4 / 3):
    """ Crop (either) the rows or columns of an image to match (as best as possible) the 
    desired ratio.
    
    Arguments:
        im (np.array): Image to be processed.
        desired_ratio (float): The desired ratio of the output image expressed as 
            width/height so 3:2 (= 3/2) or 16:9 (  = 16/9).
        
    Returns:
        An image (np.array) with the desired ratio.
    """
    height = im.shape[0]
    width = im.shape[1]
    if width / height < desired_ratio:  # Crop rows
        desired_height = int(round(width / desired_ratio))
        to_crop = height - desired_height
        top_crop = to_crop // 2
        bottom_crop = to_crop - top_crop
        cropped_image = im[top_crop:height - bottom_crop, :]
    else:  # Crop columns
        desired_width = int(round(height * desired_ratio))
        to_crop = width - desired_width
        left_crop = to_crop // 2
        right_crop = to_crop - left_crop
        cropped_image = im[:, left_crop:width - right_crop]

    return cropped_image