diff --git a/src/machinevisiontoolbox/base/graphics.py b/src/machinevisiontoolbox/base/graphics.py index 45b14afe..771bee0e 100644 --- a/src/machinevisiontoolbox/base/graphics.py +++ b/src/machinevisiontoolbox/base/graphics.py @@ -689,9 +689,15 @@ 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) + .. 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