From 2c4a6e1179a7e2460d3dae9f4558173c547996a7 Mon Sep 17 00:00:00 2001 From: void4 Date: Mon, 27 May 2024 00:23:16 +0200 Subject: [PATCH] Add function and documentation to draw circle --- docs/reference/ImageDraw.rst | 13 +++++++++++++ src/PIL/ImageDraw.py | 9 +++++++++ 2 files changed, 22 insertions(+) diff --git a/docs/reference/ImageDraw.rst b/docs/reference/ImageDraw.rst index 4ccfacae7..6987adc88 100644 --- a/docs/reference/ImageDraw.rst +++ b/docs/reference/ImageDraw.rst @@ -240,6 +240,19 @@ Methods .. versionadded:: 5.3.0 +.. py:method:: ImageDraw.circle(xy, radius, fill=None, outline=None, width=1) + + Draws a circle given the center coordinates and a radius. + + :param xy: One point to define the circle center. Sequence: + ``[x, y]`` + :param radius: Radius of the circle + :param outline: Color to use for the outline. + :param fill: Color to use for the fill. + :param width: The line width, in pixels. + + .. versionadded:: ?.?.? + .. py:method:: ImageDraw.line(xy, fill=None, width=0, joint=None) Draws a line between the coordinates in the ``xy`` list. diff --git a/src/PIL/ImageDraw.py b/src/PIL/ImageDraw.py index 42f2ee8c7..4b42b32d0 100644 --- a/src/PIL/ImageDraw.py +++ b/src/PIL/ImageDraw.py @@ -181,6 +181,15 @@ class ImageDraw: if ink is not None and ink != fill and width != 0: self.draw.draw_ellipse(xy, ink, 0, width) + def circle(self, xy: Coords, radius, fill=None, outline=None, width=1) -> None: + """Draw a circle given center coordinates and a radius.""" + ink, fill = self._getink(outline, fill) + ellipse_xy = (xy[0]-radius, xy[1]-radius, xy[0]+radius, xy[1]+radius) + if fill is not None: + self.draw.draw_ellipse(ellipse_xy, fill, 1) + if ink is not None and ink != fill and width != 0: + self.draw.draw_ellipse(ellipse_xy, ink, 0, width) + def line(self, xy: Coords, fill=None, width=0, joint=None) -> None: """Draw a line, or a connected sequence of line segments.""" ink = self._getink(fill)[0]