From db5c4fbb2cb0978f58d503aadc6901c01c023a47 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 27 Jul 2024 18:41:37 +1000 Subject: [PATCH 1/3] Include required arguments --- src/PIL/ImageDraw2.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/PIL/ImageDraw2.py b/src/PIL/ImageDraw2.py index 58302f950..6e848663b 100644 --- a/src/PIL/ImageDraw2.py +++ b/src/PIL/ImageDraw2.py @@ -80,7 +80,7 @@ class Draw: return self.image def render( - self, op: str, xy: Coords, pen: Pen | Brush, brush: Brush | Pen | None = None + self, op: str, xy: Coords, pen: Pen | Brush | None, brush: Brush | Pen | None = None ) -> None: # handle color arguments outline = fill = None @@ -111,50 +111,50 @@ class Draw: (xoffset, yoffset) = offset self.transform = (1, 0, xoffset, 0, 1, yoffset) - def arc(self, xy: Coords, start, end, *options: Any) -> None: + def arc(self, xy: Coords, pen: Pen | Brush | None, start, end, *options: Any) -> None: """ Draws an arc (a portion of a circle outline) between the start and end angles, inside the given bounding box. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.arc` """ - self.render("arc", xy, start, end, *options) + self.render("arc", xy, pen, start, end, *options) - def chord(self, xy: Coords, start, end, *options: Any) -> None: + def chord(self, xy: Coords, pen: Pen | Brush | None, start, end, *options: Any) -> None: """ Same as :py:meth:`~PIL.ImageDraw2.Draw.arc`, but connects the end points with a straight line. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.chord` """ - self.render("chord", xy, start, end, *options) + self.render("chord", xy, pen, start, end, *options) - def ellipse(self, xy: Coords, *options: Any) -> None: + def ellipse(self, xy: Coords, pen: Pen | Brush | None, *options: Any) -> None: """ Draws an ellipse inside the given bounding box. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.ellipse` """ - self.render("ellipse", xy, *options) + self.render("ellipse", xy, pen, *options) - def line(self, xy: Coords, *options: Any) -> None: + def line(self, xy: Coords, pen: Pen | Brush | None, *options: Any) -> None: """ Draws a line between the coordinates in the ``xy`` list. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.line` """ - self.render("line", xy, *options) + self.render("line", xy, pen, *options) - def pieslice(self, xy: Coords, start, end, *options: Any) -> None: + def pieslice(self, xy: Coords, pen: Pen | Brush | None, start, end, *options: Any) -> None: """ Same as arc, but also draws straight lines between the end points and the center of the bounding box. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.pieslice` """ - self.render("pieslice", xy, start, end, *options) + self.render("pieslice", xy, pen, start, end, *options) - def polygon(self, xy: Coords, *options: Any) -> None: + def polygon(self, xy: Coords, pen: Pen | Brush | None, *options: Any) -> None: """ Draws a polygon. @@ -165,15 +165,15 @@ class Draw: .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.polygon` """ - self.render("polygon", xy, *options) + self.render("polygon", xy, pen, *options) - def rectangle(self, xy: Coords, *options: Any) -> None: + def rectangle(self, xy: Coords, pen: Pen | Brush | None, *options: Any) -> None: """ Draws a rectangle. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.rectangle` """ - self.render("rectangle", xy, *options) + self.render("rectangle", xy, pen, *options) def text(self, xy: tuple[float, float], text: AnyStr, font: Font) -> None: """ From b84e2a9935a49197012002122ad84a856940133a Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 27 Jul 2024 18:55:17 +1000 Subject: [PATCH 2/3] Do not pass outline to arc --- src/PIL/ImageDraw2.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/PIL/ImageDraw2.py b/src/PIL/ImageDraw2.py index 6e848663b..97898ffd9 100644 --- a/src/PIL/ImageDraw2.py +++ b/src/PIL/ImageDraw2.py @@ -101,7 +101,9 @@ class Draw: path.transform(self.transform) xy = path # render the item - if op == "line": + if op == "arc": + self.draw.arc(xy, fill=outline) + elif op == "line": self.draw.line(xy, fill=outline, width=width) else: getattr(self.draw, op)(xy, fill=fill, outline=outline) From 955854728acf3456866bbfa152920b5d81eb449a Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 27 Jul 2024 18:44:13 +1000 Subject: [PATCH 3/3] Pass start and end to arc, chord and pieslice --- Tests/test_imagedraw2.py | 46 +++++++++++++++++++++++++++++++++++ src/PIL/ImageDraw2.py | 52 ++++++++++++++++++++++++++++++---------- 2 files changed, 86 insertions(+), 12 deletions(-) diff --git a/Tests/test_imagedraw2.py b/Tests/test_imagedraw2.py index c80aa739c..e0d368228 100644 --- a/Tests/test_imagedraw2.py +++ b/Tests/test_imagedraw2.py @@ -65,6 +65,36 @@ def test_mode() -> None: ImageDraw2.Draw("L") +@pytest.mark.parametrize("bbox", BBOX) +@pytest.mark.parametrize("start, end", ((0, 180), (0.5, 180.4))) +def test_arc(bbox: Coords, start: float, end: float) -> None: + # Arrange + im = Image.new("RGB", (W, H)) + draw = ImageDraw2.Draw(im) + pen = ImageDraw2.Pen("white", width=1) + + # Act + draw.arc(bbox, pen, start, end) + + # Assert + assert_image_similar_tofile(im, "Tests/images/imagedraw_arc.png", 1) + + +@pytest.mark.parametrize("bbox", BBOX) +def test_chord(bbox: Coords) -> None: + # Arrange + im = Image.new("RGB", (W, H)) + draw = ImageDraw2.Draw(im) + pen = ImageDraw2.Pen("yellow") + brush = ImageDraw2.Brush("red") + + # Act + draw.chord(bbox, pen, 0, 180, brush) + + # Assert + assert_image_similar_tofile(im, "Tests/images/imagedraw_chord_RGB.png", 1) + + @pytest.mark.parametrize("bbox", BBOX) def test_ellipse(bbox: Coords) -> None: # Arrange @@ -123,6 +153,22 @@ def test_line_pen_as_brush(points: Coords) -> None: assert_image_equal_tofile(im, "Tests/images/imagedraw_line.png") +@pytest.mark.parametrize("bbox", BBOX) +@pytest.mark.parametrize("start, end", ((-92, 46), (-92.2, 46.2))) +def test_pieslice(bbox: Coords, start: float, end: float) -> None: + # Arrange + im = Image.new("RGB", (W, H)) + draw = ImageDraw2.Draw(im) + pen = ImageDraw2.Pen("blue") + brush = ImageDraw2.Brush("white") + + # Act + draw.pieslice(bbox, pen, start, end, brush) + + # Assert + assert_image_similar_tofile(im, "Tests/images/imagedraw_pieslice.png", 1) + + @pytest.mark.parametrize("points", POINTS) def test_polygon(points: Coords) -> None: # Arrange diff --git a/src/PIL/ImageDraw2.py b/src/PIL/ImageDraw2.py index 97898ffd9..3d68658ed 100644 --- a/src/PIL/ImageDraw2.py +++ b/src/PIL/ImageDraw2.py @@ -80,7 +80,12 @@ class Draw: return self.image def render( - self, op: str, xy: Coords, pen: Pen | Brush | None, brush: Brush | Pen | None = None + self, + op: str, + xy: Coords, + pen: Pen | Brush | None, + brush: Brush | Pen | None = None, + **kwargs: Any, ) -> None: # handle color arguments outline = fill = None @@ -101,35 +106,51 @@ class Draw: path.transform(self.transform) xy = path # render the item - if op == "arc": - self.draw.arc(xy, fill=outline) - elif op == "line": - self.draw.line(xy, fill=outline, width=width) + if op in ("arc", "line"): + kwargs.setdefault("fill", outline) else: - getattr(self.draw, op)(xy, fill=fill, outline=outline) + kwargs.setdefault("fill", fill) + kwargs.setdefault("outline", outline) + if op == "line": + kwargs.setdefault("width", width) + getattr(self.draw, op)(xy, **kwargs) def settransform(self, offset: tuple[float, float]) -> None: """Sets a transformation offset.""" (xoffset, yoffset) = offset self.transform = (1, 0, xoffset, 0, 1, yoffset) - def arc(self, xy: Coords, pen: Pen | Brush | None, start, end, *options: Any) -> None: + def arc( + self, + xy: Coords, + pen: Pen | Brush | None, + start: float, + end: float, + *options: Any, + ) -> None: """ Draws an arc (a portion of a circle outline) between the start and end angles, inside the given bounding box. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.arc` """ - self.render("arc", xy, pen, start, end, *options) + self.render("arc", xy, pen, *options, start=start, end=end) - def chord(self, xy: Coords, pen: Pen | Brush | None, start, end, *options: Any) -> None: + def chord( + self, + xy: Coords, + pen: Pen | Brush | None, + start: float, + end: float, + *options: Any, + ) -> None: """ Same as :py:meth:`~PIL.ImageDraw2.Draw.arc`, but connects the end points with a straight line. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.chord` """ - self.render("chord", xy, pen, start, end, *options) + self.render("chord", xy, pen, *options, start=start, end=end) def ellipse(self, xy: Coords, pen: Pen | Brush | None, *options: Any) -> None: """ @@ -147,14 +168,21 @@ class Draw: """ self.render("line", xy, pen, *options) - def pieslice(self, xy: Coords, pen: Pen | Brush | None, start, end, *options: Any) -> None: + def pieslice( + self, + xy: Coords, + pen: Pen | Brush | None, + start: float, + end: float, + *options: Any, + ) -> None: """ Same as arc, but also draws straight lines between the end points and the center of the bounding box. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.pieslice` """ - self.render("pieslice", xy, pen, start, end, *options) + self.render("pieslice", xy, pen, *options, start=start, end=end) def polygon(self, xy: Coords, pen: Pen | Brush | None, *options: Any) -> None: """