def coerce_to_fixed_size_rgb(im, target_dim):
    """Convert a PIL image to a fixed size and 3 channel RGB format."""
    if im.mode not in ['RGB', 'L']:
        raise Exception(f'Unknown image mode: {im.mode}')
    resized_im = im.resize(target_dim)
    if im.mode is 'RGB':
        return resized_im
    return resized_im.convert('RGB')
