visgrep(scr: str, pat: str, tolerance: int = 0) -> int
    Visual grep of scr for pattern pat.
    Requires xautomation (http://hoopajoo.net/projects/xautomation.html).
    visgrep("screen.png", "pat.png")
    Exceptions raised: ValueError, PatternNotFound, FileNotFoundError

    :param scr: path of PNG image to be grepped.
    :param pat: path of pattern image (PNG) to look for in scr.
    :param tolerance: An integer ≥ 0 to specify the level of tolerance for 'fuzzy' matches. If negative or not convertible to int, raises ValueError.
    :returns: coordinates of the topleft point of the match, if any. Raises PatternNotFound exception otherwise.

def visgrep(scr: str, pat: str, tolerance: int = 0) -> int:
    """
    visgrep(scr: str, pat: str, tolerance: int = 0) -> int
    Visual grep of scr for pattern pat.
    Requires xautomation (http://hoopajoo.net/projects/xautomation.html).
    visgrep("screen.png", "pat.png")
    Exceptions raised: ValueError, PatternNotFound, FileNotFoundError

    :param scr: path of PNG image to be grepped.
    :param pat: path of pattern image (PNG) to look for in scr.
    :param tolerance: An integer ≥ 0 to specify the level of tolerance for 'fuzzy' matches. If negative or not convertible to int, raises ValueError.
    :returns: coordinates of the topleft point of the match, if any. Raises PatternNotFound exception otherwise.
    """
    tol = int(tolerance)
    if tol < 0:
        raise ValueError("tolerance must be ≥ 0.")
    with open(scr), open(pat):
        pass
    with tempfile.NamedTemporaryFile() as f:
        subprocess.call(['png2pat', pat], stdout=f)
        # don't use check_call, some versions (1.05) have a missing return statement in png2pat.c so the exit status ≠ 0
        f.flush()
        os.fsync(f.fileno())
        vg = subprocess.Popen(['visgrep', '-t' + str(tol), scr, f.name], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        out = vg.communicate()
    coord_str = out[0].decode().split(' ')[0].split(',')
    try:
        coord = [int(coord_str[0]), int(coord_str[1])]
    except (ValueError, IndexError) as e:
        raise PatternNotFound(str([x.decode() for x in out]) + '\n\t' + repr(e))
    return coord