#!/usr/bin/env python3 import argparse from pathlib import Path import cv2 as cv parser = argparse.ArgumentParser(description="Detect and decode one QR code with OpenCV.") parser.add_argument("input_image", type=Path) parser.add_argument("output_image", type=Path) args = parser.parse_args() image = cv.imread(str(args.input_image)) if image is None: raise SystemExit(f"could not read image: {args.input_image}") detector = cv.QRCodeDetector() payload, points, straight_qrcode = detector.detectAndDecode(image) if points is None or not payload: raise SystemExit("QR code not detected or decoded") corners = points.reshape(-1, 2) annotated = image.copy() cv.polylines(annotated, [corners.astype(int)], isClosed=True, color=(0, 255, 0), thickness=3) for index, (x_coord, y_coord) in enumerate(corners, start=1): point = (int(round(x_coord)), int(round(y_coord))) cv.circle(annotated, point, 6, (0, 0, 255), -1) cv.putText( annotated, str(index), (point[0] + 8, point[1] - 8), cv.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2, cv.LINE_AA, ) args.output_image.parent.mkdir(parents=True, exist_ok=True) if not cv.imwrite(str(args.output_image), annotated): raise SystemExit(f"could not write image: {args.output_image}") print(f"payload: {payload}") print("corners:") for index, (x_coord, y_coord) in enumerate(corners, start=1): print(f" {index}: ({x_coord:.1f}, {y_coord:.1f})") if straight_qrcode is not None and straight_qrcode.size: height, width = straight_qrcode.shape[:2] print(f"straight QR size: {width}x{height}") print(f"output: {args.output_image}")