def desaturate(img):
    """Desaturate an RGB or BGR image

    Receive an RGB or BGR image as a 3D uint8 numpy array. Custom
    desaturate by keeping only the value of the darkest pixel in any of
    the channels. White background and black ink are therefore not
    affected but even very bright ink of commonly used colors (namely
    blue and red but also valid for green, cyan, magenta and even yellow)
    in the same image is desaturated to near black to facilitate
    thresholding. Return a single-channel image as a 2D uint8 numpy array.

    Note: Specifically for text detection against a white-balanced
    background this should be the preferred desaturating method followed
    by custom thresholding even (especially) if the intent is to use the
    thresholded result by OpenCV contour detection or similar algorithms.
    """

    return img.min(axis=2)