There's a fix for this upstream, merged but not released.
see https://github.com/python-pillow/Pillow/pull/2624
diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py
index 12b687c3b..fde312f44 100644
--- a/Tests/test_imagefont.py
+++ b/Tests/test_imagefont.py
@@ -393,6 +393,11 @@ def test_default_font(self):
# Assert
self.assert_image_equal(im, target_img)
+ def test_getsize_empty(self):
+ font = self.get_font()
+ # should not crash.
+ self.assertEqual((0, 0), font.getsize(''))
+
def _test_fake_loading_font(self, path_to_fake, fontname):
# Make a copy of FreeTypeFont so we can patch the original
free_type_font = copy.deepcopy(ImageFont.FreeTypeFont)
diff --git a/_imagingft.c b/_imagingft.c
index cd17758e4..3eb8bc0b7 100644
--- a/_imagingft.c
+++ b/_imagingft.c
@@ -109,7 +109,7 @@ getfont(PyObject* self_, PyObject* args, PyObject* kw)
unsigned char* font_bytes;
int font_bytes_size = 0;
static char* kwlist[] = {
- "filename", "size", "index", "encoding", "font_bytes",
+ "filename", "size", "index", "encoding", "font_bytes",
"layout_engine", NULL
};
@@ -449,8 +449,6 @@ font_getsize(FontObject* self, PyObject* args)
y_max = y_min = 0;
count = text_layout(string, self, dir, features, &glyph_info, 0);
- if (count == 0)
- return NULL;
for (x = i = 0; i < count; i++) {
int index, error;
On 08/22/2017 08:56 AM, Aaron Toponce wrote:
Package: python-pil
Version: 4.2.1-1
When using the text() function with the PIL ImageDraw module, if the text
contains a blankline (IE: "\n\n"), PIL crashes.
However, if horizontal whitespace exists in the blank line, the module will not
error out. But if the blank line is excactly "\n\n", without any horizontal
whitespace, an exception is caught, and the ImageDraw module crashes with the
ImageDraw Draw.text() function.
Below is an interactive session in ipython(1). Notice the "\n\n" on input 19,
versus "\n" on input 7 and "\n \n" on input 18:
In [1]: from PIL import Image
In [2]: from PIL import ImageDraw
In [3]: from PIL import ImageFont
In [4]: from textwrap import TextWrapper
In [5]: fontfile =
"/usr/share/fonts/truetype/dejavu/DejaVuSansMono-Bold.ttf"
In [6]: font = ImageFont.truetype(fontfile, 24)
In [7]: text = u'FOO\nBAR\nBAZ' # succeeds
In [8]: tw = TextWrapper()
In [9]: tw.width = 30
In [10]: num_lines = text.count("\n")
In [11]: height = 52 + (26 * num_lines) + 52
In [12]: background = Image.new("RGB", (500, max(160, height)), (255, 255,
255))
In [13]: img = Image.open("/home/aaron/src/ciphermonkey/images/banana.png")
In [14]: background.paste(img, (20, 10))
In [15]: draw = ImageDraw.Draw(background)
In [16]: draw.text((20, 52), text, (0, 0, 0), font=font)
In [17]: text = u'FOO\nBAR\n \nBAZ' # succeeds
In [18]: draw.text((20, 52), text, (0, 0, 0), font=font)
In [19]: text = u'FOO\nBAR\n\nBAZ' # fails
In [20]: draw.text((20, 52), text, (0, 0, 0), font=font)
---------------------------------------------------------------------------
SystemError Traceback (most recent call last)
<ipython-input-20-7219ebe64fdf> in <module>()
----> 1 draw.text((20, 52), text, (0, 0, 0), font=font)
/usr/lib/python2.7/dist-packages/PIL/ImageDraw.pyc in text(self, xy, text, fill, font, anchor, *args, **kwargs)
207 if self._multiline_check(text):
208 return self.multiline_text(xy, text, fill, font,
anchor,
--> 209 *args, **kwargs)
210 ink, fill = self._getink(fill)
211 if font is None:
/usr/lib/python2.7/dist-packages/PIL/ImageDraw.pyc in multiline_text(self, xy, text, fill, font, anchor, spacing, align, direction, features)
231 line_spacing = self.textsize('A', font=font)[1] + spacing
232 for line in lines:
--> 233 line_width, line_height = self.textsize(line, font)
234 widths.append(line_width)
235 max_width = max(max_width, line_width)
/usr/lib/python2.7/dist-packages/PIL/ImageDraw.pyc in textsize(self, text, font, spacing, direction, features)
258 if font is None:
259 font = self.getfont()
--> 260 return font.getsize(text, direction, features)
261
262 def multiline_textsize(self, text, font=None, spacing=4,
direction=None,
/usr/lib/python2.7/dist-packages/PIL/ImageFont.pyc in getsize(self, text, direction, features)
154
155 def getsize(self, text, direction=None, features=None):
--> 156 size, offset = self.font.getsize(text, direction, features)
157 return (size[0] + offset[0], size[1] + offset[1])
158
SystemError: error return without exception set