image = cv.imread(args.image, cv.IMREAD_COLOR) if image is None: raise SystemExit(f"Could not read image: {args.image}") image_height, image_width = image.shape[:2] rect = parse_values(args.rect, 4, "--rect x,y,width,height") foreground_points = [ parse_values(value, 2, "--foreground-point x,y") for value in args.foreground_point ] foreground_strokes = [ parse_values(value, 5, "--foreground-stroke x1,y1,x2,y2,thickness") for value in args.foreground_stroke ] exclude_rect = parse_values(args.exclude_rect, 4, "--exclude-rect x,y,width,height") for label, (x, y, width, height) in (("--rect", rect), ("--exclude-rect", exclude_rect)): if x < 0 or y < 0 or width <= 0 or height <= 0: raise SystemExit(f"{label} width and height must be positive.") if x + width > image_width or y + height > image_height: raise SystemExit(f"{label} extends beyond the {image_width}x{image_height} image.") for point_x, point_y in foreground_points: if not (0 <= point_x < image_width and 0 <= point_y < image_height): raise SystemExit(f"--foreground-point extends beyond the {image_width}x{image_height} image.") for start_x, start_y, end_x, end_y, thickness in foreground_strokes: if thickness <= 0: raise SystemExit("--foreground-stroke thickness must be positive.") if not (0 <= start_x < image_width and 0 <= start_y < image_height): raise SystemExit(f"--foreground-stroke extends beyond the {image_width}x{image_height} image.") if not (0 <= end_x < image_width and 0 <= end_y < image_height): raise SystemExit(f"--foreground-stroke extends beyond the {image_width}x{image_height} image.")