From f586adefd7880a102349ec642d4247b827341109 Mon Sep 17 00:00:00 2001 From: hugovk Date: Wed, 4 Oct 2017 09:54:53 +0300 Subject: [PATCH 1/2] Failing test for #2783 --- Tests/test_imagedraw.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Tests/test_imagedraw.py b/Tests/test_imagedraw.py index e4bcb9faa..a79a75ca0 100644 --- a/Tests/test_imagedraw.py +++ b/Tests/test_imagedraw.py @@ -380,7 +380,6 @@ class TestImageDraw(PillowTestCase): self.assert_image_equal( im, Image.open("Tests/images/imagedraw_floodfill2.png")) - def test_floodfill_thresh(self): # floodfill() is experimental @@ -560,6 +559,19 @@ class TestImageDraw(PillowTestCase): # Assert self.assert_image_similar(im, Image.open(expected), 1) + def test_textsize_empty_string(self): + # https://github.com/python-pillow/Pillow/issues/2783 + # Arrange + im = Image.new("RGB", (W, H)) + draw = ImageDraw.Draw(im) + + # Act + # Should not cause 'SystemError: returned NULL without setting an error' + draw.textsize("") + draw.textsize("\n") + draw.textsize("test\n") + if __name__ == '__main__': unittest.main() From 3482f23f62cd3f640106c3baba6acfa4bf978a6b Mon Sep 17 00:00:00 2001 From: hugovk Date: Wed, 4 Oct 2017 09:56:57 +0300 Subject: [PATCH 2/2] Empty strings are zero-size --- PIL/ImageDraw.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/PIL/ImageDraw.py b/PIL/ImageDraw.py index 89df27338..83e4d458c 100644 --- a/PIL/ImageDraw.py +++ b/PIL/ImageDraw.py @@ -217,7 +217,8 @@ class ImageDraw(object): ink = fill if ink is not None: try: - mask, offset = font.getmask2(text, self.fontmode, *args, **kwargs) + mask, offset = font.getmask2(text, self.fontmode, + *args, **kwargs) xy = xy[0] + offset[0], xy[1] + offset[1] except AttributeError: try: @@ -254,6 +255,8 @@ class ImageDraw(object): def textsize(self, text, font=None, spacing=4, direction=None, features=None): """Get the size of a given string, in pixels.""" + if len(text) == 0: + return (0, 0) if self._multiline_check(text): return self.multiline_textsize(text, font, spacing, direction, features) @@ -383,4 +386,4 @@ def _color_diff(rgb1, rgb2): """ Uses 1-norm distance to calculate difference between two rgb values. """ - return abs(rgb1[0]-rgb2[0]) + abs(rgb1[1]-rgb2[1]) + abs(rgb1[2]-rgb2[2]) + return abs(rgb1[0]-rgb2[0]) + abs(rgb1[1]-rgb2[1]) + abs(rgb1[2]-rgb2[2])