def is_transparent(im):
    """
    Detect if an image has any transparent components.

    Based on https://stackoverflow.com/a/58567453
    """

    if im.mode == "P":
        transparent = im.info.get("transparency", -1)
        for _, index in im.getcolors():
            if index == transparent:
                return True
    elif im.mode == "RGBA":
        extrema = im.getextrema()
        if extrema[3][0] < 255:
            return True

    return False