From 99e6310dfe0d4d75479c0332e73306666a015c68 Mon Sep 17 00:00:00 2001 From: Peter Corke Date: Tue, 24 Nov 2026 06:31:53 +1000 Subject: [PATCH 1/2] fix: say what is wrong with the marker string of draw_point Two color letters, no marker character and no color at all each failed with an unrelated TypeError or ValueError from deeper in the code. Raise ValueError with a message for each. Co-Authored-By: Claude Sonnet 5.5 --- src/machinevisiontoolbox/base/graphics.py | 22 ++++++++++++ tests/base/test_draw_point_errors.py | 43 +++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 tests/base/test_draw_point_errors.py diff --git a/src/machinevisiontoolbox/base/graphics.py b/src/machinevisiontoolbox/base/graphics.py index 1b1d8f12..e33d1793 100644 --- a/src/machinevisiontoolbox/base/graphics.py +++ b/src/machinevisiontoolbox/base/graphics.py @@ -453,6 +453,8 @@ def draw_labelbox( :type fontthickness: int, optional :param boxargs: arguments passed to :func:`draw_box` :raises TypeError: can't draw color into a greyscale image + :raises ValueError: if the marker string has more than one color letter, has no + marker character, or no color is given by ``color`` or by a letter :return: passed image as modified :rtype: ndarray(H,W), ndarray(H,W,P) @@ -692,6 +694,10 @@ def draw_point( :return: passed image as modified :rtype: ndarray(H,W), ndarray(H,W,P) + .. versionchanged:: 2.5.0 + These cases raise ``ValueError`` with a message; they raised ``TypeError`` + or ``ValueError`` from deeper in the code. + The ``text`` label is placed to the right of the marker, and vertically centred. The color of the marker can be different to the color of the text, the marker color is specified by a single letter in the marker string, eg. 'b+'. @@ -793,6 +799,7 @@ def draw_point( f"got {type(pos).__name__}" ) + marker_in = marker newmarker = "" markercolor = "" for m in marker: @@ -802,6 +809,21 @@ def draw_point( newmarker += m marker = newmarker + if len(markercolor) > 1: + raise ValueError( + f"marker has more than one color letter, got {markercolor!r} in the " + f"marker string {marker_in!r}" + ) + if marker == "": + raise ValueError( + f"marker string {marker_in!r} has no marker character, such as '+' or '*'" + ) + if color is None and markercolor == "": + raise ValueError( + "no color: give color=, or a color letter in the marker string, " + "such as 'r+'" + ) + # get the centre of the marker, cv2.getTextSize is a very loose bounding box # the code below is a bit expensive but the only way to precisely position # the marker diff --git a/tests/base/test_draw_point_errors.py b/tests/base/test_draw_point_errors.py new file mode 100644 index 00000000..b08d3f23 --- /dev/null +++ b/tests/base/test_draw_point_errors.py @@ -0,0 +1,43 @@ +"""draw_point() says what is wrong with its marker string.""" + +import numpy as np +import pytest + +from machinevisiontoolbox.base.graphics import draw_point + + +@pytest.fixture +def img(): + return np.zeros((60, 60, 3), dtype="uint8") + + +@pytest.mark.parametrize("marker", ["rg+", "rr+", "kw*", "+bk"]) +def test_two_color_letters(img, marker): + with pytest.raises(ValueError, match="more than one color letter"): + draw_point(img, (30, 30), marker, fontsize=1) + + +@pytest.mark.parametrize("marker", ["", "r", "g"]) +def test_no_marker_character(img, marker): + with pytest.raises(ValueError, match="no marker character"): + draw_point(img, (30, 30), marker, color="red", fontsize=1) + + +@pytest.mark.parametrize("marker", ["+", "*", "x"]) +def test_no_color_at_all(img, marker): + with pytest.raises(ValueError, match="no color"): + draw_point(img, (30, 30), marker, fontsize=1) + + +@pytest.mark.parametrize( + "marker, color", + [("r+", None), ("+", "red"), ("b*", None), ("*", (0, 255, 0)), ("x", 200)], +) +def test_valid_calls_still_draw(img, marker, color): + draw_point(img, (30, 30), marker, color=color, fontsize=1) + assert img.any() + + +def test_color_argument_wins_over_the_letter(img): + draw_point(img, (30, 30), "r+", color="blue", fontsize=1) + assert img[..., 2].max() > 0 and img[..., 0].max() == 0 From b64570ac28202d26112a123f8d79e3cc518adce9 Mon Sep 17 00:00:00 2001 From: Peter Corke Date: Tue, 24 Nov 2026 06:32:18 +1000 Subject: [PATCH 2/2] docs: put the draw_point :raises: note in draw_point, not draw_labelbox Co-Authored-By: Claude Sonnet 5.5 --- src/machinevisiontoolbox/base/graphics.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/machinevisiontoolbox/base/graphics.py b/src/machinevisiontoolbox/base/graphics.py index e33d1793..eeead0cf 100644 --- a/src/machinevisiontoolbox/base/graphics.py +++ b/src/machinevisiontoolbox/base/graphics.py @@ -453,8 +453,6 @@ def draw_labelbox( :type fontthickness: int, optional :param boxargs: arguments passed to :func:`draw_box` :raises TypeError: can't draw color into a greyscale image - :raises ValueError: if the marker string has more than one color letter, has no - marker character, or no color is given by ``color`` or by a letter :return: passed image as modified :rtype: ndarray(H,W), ndarray(H,W,P) @@ -691,6 +689,8 @@ def draw_point( :param fontthickness: font thickness in pixels, defaults to 2 :type fontthickness: int, optional :raises TypeError: can't draw color into a greyscale image + :raises ValueError: if the marker string has more than one color letter, has no + marker character, or no color is given by ``color`` or by a letter :return: passed image as modified :rtype: ndarray(H,W), ndarray(H,W,P)