diff --git a/Tests/test_imagedraw2.py b/Tests/test_imagedraw2.py index 35fe0cf40dd..c80aa739cdf 100644 --- a/Tests/test_imagedraw2.py +++ b/Tests/test_imagedraw2.py @@ -57,6 +57,14 @@ def test_sanity() -> None: draw2.line(list(range(10)), pen) +def test_mode() -> None: + draw = ImageDraw2.Draw("L", (1, 1)) + assert draw.image.mode == "L" + + with pytest.raises(ValueError): + ImageDraw2.Draw("L") + + @pytest.mark.parametrize("bbox", BBOX) def test_ellipse(bbox: Coords) -> None: # Arrange diff --git a/src/PIL/ImageDraw2.py b/src/PIL/ImageDraw2.py index b42f5d9eac1..600e140e1f6 100644 --- a/src/PIL/ImageDraw2.py +++ b/src/PIL/ImageDraw2.py @@ -56,8 +56,16 @@ class Draw: (Experimental) WCK-style drawing interface """ - def __init__(self, image, size=None, color=None): - if not hasattr(image, "im"): + def __init__( + self, + image: Image.Image | str, + size: tuple[int, int] | list[int] | None = None, + color: float | tuple[float, ...] | str | None = None, + ) -> None: + if isinstance(image, str): + if size is None: + msg = "If image argument is mode string, size must be a list or tuple" + raise ValueError(msg) image = Image.new(image, size, color) self.draw = ImageDraw.Draw(image) self.image = image