From c91bd27eec97bf6a05f85393af1fdff143339369 Mon Sep 17 00:00:00 2001 From: hugovk Date: Thu, 17 Oct 2013 17:36:38 +0300 Subject: [PATCH 1/2] Add tests for rotated and non-rotated transposed font Check the box size of an ImageFont.TransposedFont matches that of the original ImageFont. --- Tests/test_imagefont.py | 47 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index 85940d087..9ac2cdd89 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -88,5 +88,48 @@ def test_render_multiline(): # setting a tight epsilon, I'm showing the original test failure # at epsilon = ~38. assert_image_similar(im, target_img,.5) - - + + +def test_rotated_transposed_font(): + img_grey = Image.new("L", (100, 100)) + draw = ImageDraw.Draw(img_grey) + word = "testing" + font = ImageFont.truetype(font_path, font_size) + + orientation = Image.ROTATE_90 + transposed_font = ImageFont.TransposedFont(font, orientation=orientation) + + # Original font + draw.setfont(font) + box_size_a = draw.textsize(word) + + # Rotated font + draw.setfont(transposed_font) + box_size_b = draw.textsize(word) + + # Check (w,h) of box a is (h,w) of box b + assert_equal(box_size_a[0], box_size_b[1]) + assert_equal(box_size_a[1], box_size_b[0]) + + +def test_unrotated_transposed_font(): + img_grey = Image.new("L", (100, 100)) + draw = ImageDraw.Draw(img_grey) + word = "testing" + font = ImageFont.truetype(font_path, font_size) + + orientation = None + transposed_font = ImageFont.TransposedFont(font, orientation=orientation) + + # Original font + draw.setfont(font) + box_size_a = draw.textsize(word) + + # Rotated font + draw.setfont(transposed_font) + box_size_b = draw.textsize(word) + + # Check boxes a and b are same size + assert_equal(box_size_a, box_size_b) + + From 92507e5d0a692ffc3a5e61f0302a4d29ba3d6d42 Mon Sep 17 00:00:00 2001 From: hugovk Date: Thu, 17 Oct 2013 19:00:42 +0300 Subject: [PATCH 2/2] Fix issue #382: TypeError with TransposedFont's getsize() When getting the size of text with a TransposedFont it was failing: File "/usr/local/lib/python2.7/site-packages/PIL/ImageDraw.py", line 281, in textsize return font.getsize(text) File "/usr/local/lib/python2.7/site-packages/PIL/ImageFont.py", line 189, in getsize w, h = self.font.getsize(text)[0] TypeError: 'int' object is not iterable This is because self.font.getsize(text) returns a (w, h) tuple. To fix, remove the [0]. Test cases have been created in test_imagefont.py: test_rotated_transposed_font() test_unrotated_transposed_font() Both fail before the fix, both pass with the fix. Furthermore, the code I'm using this from ( https://github.com/mattdeboard/word_cloud ) now works as expected and creates a word cloud similar to the ones that PIL created. --- PIL/ImageFont.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PIL/ImageFont.py b/PIL/ImageFont.py index 8313ee3ba..9366937dd 100644 --- a/PIL/ImageFont.py +++ b/PIL/ImageFont.py @@ -171,7 +171,7 @@ class TransposedFont: self.orientation = orientation # any 'transpose' argument, or None def getsize(self, text): - w, h = self.font.getsize(text)[0] + w, h = self.font.getsize(text) if self.orientation in (Image.ROTATE_90, Image.ROTATE_270): return h, w return w, h