Allow lists as individual ImageDraw co-ordinates

This commit is contained in:
Andrew Murray 2019-03-23 09:04:20 +11:00
parent e6db8dee0c
commit 0469be3d49
2 changed files with 32 additions and 0 deletions

View File

@ -762,3 +762,22 @@ class TestImageDraw(PillowTestCase):
expected = ("Tests/images/imagedraw_outline" expected = ("Tests/images/imagedraw_outline"
"_{}_{}.png".format(operation, mode)) "_{}_{}.png".format(operation, mode))
self.assert_image_similar(im, Image.open(expected), 1) self.assert_image_similar(im, Image.open(expected), 1)
def test_xy(self):
im = hopper()
draw = ImageDraw.Draw(im)
for xy in [
[(X0, Y0), (X1, Y1)],
[[X0, Y0], [X1, Y1]],
[X0, Y0, X1, Y1]
]:
draw.arc(xy, 0, 90)
draw.chord(xy, 0, 90)
draw.pieslice(xy, 0, 90)
draw.ellipse(xy)
draw.line(xy)
draw.point(xy)
draw.polygon(xy)
draw.rectangle(xy)

View File

@ -98,6 +98,10 @@ class ImageDraw(object):
self.font = ImageFont.load_default() self.font = ImageFont.load_default()
return self.font return self.font
def _getxy(self, xy):
return [tuple(coord) if isinstance(coord, list) else coord
for coord in xy]
def _getink(self, ink, fill=None): def _getink(self, ink, fill=None):
if ink is None and fill is None: if ink is None and fill is None:
if self.fill: if self.fill:
@ -121,6 +125,7 @@ class ImageDraw(object):
def arc(self, xy, start, end, fill=None, width=0): def arc(self, xy, start, end, fill=None, width=0):
"""Draw an arc.""" """Draw an arc."""
xy = self._getxy(xy)
ink, fill = self._getink(fill) ink, fill = self._getink(fill)
if ink is not None: if ink is not None:
self.draw.draw_arc(xy, start, end, ink, width) self.draw.draw_arc(xy, start, end, ink, width)
@ -136,6 +141,7 @@ class ImageDraw(object):
def chord(self, xy, start, end, fill=None, outline=None, width=0): def chord(self, xy, start, end, fill=None, outline=None, width=0):
"""Draw a chord.""" """Draw a chord."""
xy = self._getxy(xy)
ink, fill = self._getink(outline, fill) ink, fill = self._getink(outline, fill)
if fill is not None: if fill is not None:
self.draw.draw_chord(xy, start, end, fill, 1) self.draw.draw_chord(xy, start, end, fill, 1)
@ -144,6 +150,7 @@ class ImageDraw(object):
def ellipse(self, xy, fill=None, outline=None, width=0): def ellipse(self, xy, fill=None, outline=None, width=0):
"""Draw an ellipse.""" """Draw an ellipse."""
xy = self._getxy(xy)
ink, fill = self._getink(outline, fill) ink, fill = self._getink(outline, fill)
if fill is not None: if fill is not None:
self.draw.draw_ellipse(xy, fill, 1) self.draw.draw_ellipse(xy, fill, 1)
@ -152,6 +159,7 @@ class ImageDraw(object):
def line(self, xy, fill=None, width=0, joint=None): def line(self, xy, fill=None, width=0, joint=None):
"""Draw a line, or a connected sequence of line segments.""" """Draw a line, or a connected sequence of line segments."""
xy = self._getxy(xy)
ink = self._getink(fill)[0] ink = self._getink(fill)[0]
if ink is not None: if ink is not None:
self.draw.draw_lines(xy, ink, width) self.draw.draw_lines(xy, ink, width)
@ -220,6 +228,7 @@ class ImageDraw(object):
def pieslice(self, xy, start, end, fill=None, outline=None, width=0): def pieslice(self, xy, start, end, fill=None, outline=None, width=0):
"""Draw a pieslice.""" """Draw a pieslice."""
xy = self._getxy(xy)
ink, fill = self._getink(outline, fill) ink, fill = self._getink(outline, fill)
if fill is not None: if fill is not None:
self.draw.draw_pieslice(xy, start, end, fill, 1) self.draw.draw_pieslice(xy, start, end, fill, 1)
@ -228,12 +237,14 @@ class ImageDraw(object):
def point(self, xy, fill=None): def point(self, xy, fill=None):
"""Draw one or more individual pixels.""" """Draw one or more individual pixels."""
xy = self._getxy(xy)
ink, fill = self._getink(fill) ink, fill = self._getink(fill)
if ink is not None: if ink is not None:
self.draw.draw_points(xy, ink) self.draw.draw_points(xy, ink)
def polygon(self, xy, fill=None, outline=None): def polygon(self, xy, fill=None, outline=None):
"""Draw a polygon.""" """Draw a polygon."""
xy = self._getxy(xy)
ink, fill = self._getink(outline, fill) ink, fill = self._getink(outline, fill)
if fill is not None: if fill is not None:
self.draw.draw_polygon(xy, fill, 1) self.draw.draw_polygon(xy, fill, 1)
@ -242,6 +253,7 @@ class ImageDraw(object):
def rectangle(self, xy, fill=None, outline=None, width=0): def rectangle(self, xy, fill=None, outline=None, width=0):
"""Draw a rectangle.""" """Draw a rectangle."""
xy = self._getxy(xy)
ink, fill = self._getink(outline, fill) ink, fill = self._getink(outline, fill)
if fill is not None: if fill is not None:
self.draw.draw_rectangle(xy, fill, 1) self.draw.draw_rectangle(xy, fill, 1)
@ -264,6 +276,7 @@ class ImageDraw(object):
if self._multiline_check(text): if self._multiline_check(text):
return self.multiline_text(xy, text, fill, font, anchor, return self.multiline_text(xy, text, fill, font, anchor,
*args, **kwargs) *args, **kwargs)
xy = self._getxy(xy)
ink, fill = self._getink(fill) ink, fill = self._getink(fill)
if font is None: if font is None:
font = self.getfont() font = self.getfont()