<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def mrz():
 """ Command-line script for extracting MRZ from a given image """

    parser = argparse.ArgumentParser(description='Run the MRZ OCR recognition algorithm on the given image.')
    parser.add_argument('filename')
    parser.add_argument('--json', action='store_true', help='Produce JSON (rather than tabular) output')
    parser.add_argument('--legacy', action='store_true',
                        help='Use the "legacy" Tesseract OCR engine (--oem 0). Despite the name, it most often results in better '
                        'results. It is not the default option, because it will only work if '
                        'your Tesseract installation includes the legacy *.traineddata files. You can download them at '
                        'https://github.com/tesseract-ocr/tesseract/wiki/Data-Files#data-files-for-version-400-november-29-2016')
    parser.add_argument('-r', '--save-roi', default=None,
                        help='Output the region of the image that is detected to contain the MRZ to the given png file')
    parser.add_argument('--version', action='version', version='PassportEye MRZ v%s' % passporteye.__version__)
    args = parser.parse_args()

    try:
        extra_params = '--oem 0' if args.legacy else ''
        filename, mrz_, walltime = process_file((args.filename, args.save_roi is not None, extra_params))
    except TesseractNotFoundError:
        sys.stderr.write("ERROR: The tesseract executable was not found.\n"
                         "Please, make sure Tesseract is installed and the appropriate directory is included "
                         "in your PATH environment variable.\n")
        sys.exit(1)
    except TesseractError as ex:
        sys.stderr.write("ERROR: %s" % ex.message)
        sys.exit(ex.status)
    
    d = mrz_.to_dict() if mrz_ is not None else {'mrz_type': None, 'valid': False, 'valid_score': 0}
    d['walltime'] = walltime
    d['filename'] = filename

    if args.save_roi is not None and mrz_ is not None and 'roi' in mrz_.aux:
        io.imsave(args.save_roi, mrz_.aux['roi'])

    if not args.json:
        for k in d:
            print("%s\t%s" % (k, str(d[k])))
    else:
        print(json.dumps(d, indent=2))