Example #1
0
def _normalise_format(path, format, header=None):
    """Extracts ``"JPEG"``, ``"PNG"`` etc from arguments provided to image
    read write methods. Implements the hierarchy for multiple arguments given.

    - User explicitly gives format.
    - Format read from header.
    - Format read from filename suffix.

    """
    if format is None:
        if header is not None:
            format = format_from_header(header)

    if format is None:
        try:
            format = Path(path).suffix
        except TypeError:
            raise ValueError(
                "No `format` argument was given and couldn't guess the format from `path`."
            )

    format = format.upper()
    if format[0] == ".":
        format = format[1:]
    if format == "JPG":
        format = "JPEG"
    if format == "TIF":
        format = "TIFF"
    return format