def flatten(image, char):
    """
    Given a layered image (typically (y, x, RGB)), return a plain 2D image
    (y, x) according to a spec.

    Args:
        image (np.ndarray): The image to flatten
        char (char): One of (R, G, B, or V (=value))

    Returns:
        np.ndarray - The 2D image.
    """
    if image.ndim < 3:
        return image
    char2idx = dict(R=0, G=1, B=2)
    ret = None
    if char == "V":
        ret = image.mean(axis=2)
    elif char in char2idx:
        ret = image[:, :, char2idx[char]]
    else:
        # Shouldn't happen
        assert False, "Unhandled - invalid flat spec '%s'" % char
    return ret