def main()

in pdq/python/pdqhashing/tools/pdq_photo_hasher_tool.py [0:0]


    def main(cls, args):
        parser = argparse.ArgumentParser(
            prog=cls.PROGNAME,
            description="Create PDQ Photo hashes for provided files. "
            + "Supported filetypes are: JPEG and PNG.",
        )

        parser.add_argument(
            "filenames",
            nargs="*",
            type=str,
            help="Filenames/paths of the files to be processed.",
        )

        parser.add_argument(
            "-i",
            "--filesOnStdin",
            action="store_true",
            help=(
                "Take filenames from stdin, in which case there must be "
                + "no filenames on the command line."
            ),
        )

        parser.add_argument(
            "-d",
            "--doDetailedOutput",
            action="store_true",
            help="Print norm, delta, etc; Otherwise, print just hash, "
            + "quality and filename.",
        )

        parser.add_argument(
            "--pdq",
            dest="doPDQ",
            action="store_true",
            help=(
                "Take filenames from stdin, in which case there must be "
                + "no filenames on the command line."
            ),
        )

        parser.add_argument(
            "--pdqdih",
            dest="doPDQDih",
            action="store_true",
            help="Print all 8 dihedral-transform hashes.",
        )

        parser.add_argument(
            "--pdqdih-across",
            dest="doPDQDihAcross",
            action="store_true",
            help="Print all 8 dihedral-transform hashes, all on one line.",
        )

        parser.add_argument(
            "--no-timings",
            dest="doTimings",
            action="store_false",
            help="Do not compute timing information.",
        )

        parser.add_argument(
            "--k",
            dest="keepGoingAfterErrors",
            action="store_true",
            help="Continue to process next image in case of errors",
        )

        args = parser.parse_args()

        pdqHasher = PDQHasher()
        context = cls.Context(0, None, False)
        # Iterate over image-file names. One file at a time, compute per-file
        # hash and hamming distance to previous.
        if args.filesOnStdin:
            if args.filenames:
                parser.print_help()
                exit(1)
            try:
                lno = 0
                for filename in sys.stdin:
                    lno += 1
                    context.numPDQHash += 1
                    cls.processFile(
                        pdqHasher,
                        filename.strip(),
                        args.doPDQ,
                        args.doPDQDih,
                        args.doPDQDihAcross,
                        args.doDetailedOutput,
                        args.doTimings,
                        args.keepGoingAfterErrors,
                        context,
                    )
                    sys.stdout.flush()
                exit(0)
            except IOError:
                sys.stderr.write(
                    "{}: couldn't read line {} \n".format(cls.PROGNAME, lno)
                )
                exit(1)

        for filename in args.filenames:
            context.numPDQHash += 1
            cls.processFile(
                pdqHasher,
                filename,
                args.doPDQ,
                args.doPDQDih,
                args.doPDQDihAcross,
                args.doDetailedOutput,
                args.doTimings,
                args.keepGoingAfterErrors,
                context,
            )
            sys.stdout.flush()
        if context.hadError:
            exit(1)