From 295382aadc994dc81e5076450dce5cf573a9bbd0 Mon Sep 17 00:00:00 2001 From: homm Date: Fri, 2 Dec 2016 15:40:32 +0300 Subject: [PATCH] vertical and horizontal pass with boxes --- PIL/Image.py | 13 +++++++----- Tests/test_image_resample.py | 40 ++++++++++++++++++++++++++++-------- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/PIL/Image.py b/PIL/Image.py index 844f27ab5..a3171aeaa 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -1544,10 +1544,14 @@ class Image(object): ): raise ValueError("unknown resampling filter") - self.load() - size = tuple(size) - if self.size == size: + + if box is None: + box = (0, 0) + self.size + else: + box = tuple(box) + + if self.size == size and box == (0, 0) + self.size: return self.copy() if self.mode in ("1", "P"): @@ -1559,8 +1563,7 @@ class Image(object): if self.mode == 'RGBA': return self.convert('RGBa').resize(size, resample).convert('RGBA') - if box is None: - box = (0, 0) + self.size + self.load() return self._new(self.im.resize(size, resample, box)) diff --git a/Tests/test_image_resample.py b/Tests/test_image_resample.py index a3e507745..632ead76c 100644 --- a/Tests/test_image_resample.py +++ b/Tests/test_image_resample.py @@ -1,5 +1,7 @@ from __future__ import division +from contextlib import contextmanager + from helper import unittest, PillowTestCase, hopper from PIL import Image, ImageDraw, ImageMode @@ -303,24 +305,44 @@ class CoreResampleAlphaCorrectTest(PillowTestCase): class CoreResamplePassesTest(PillowTestCase): + @contextmanager + def count(self, diff): + count = Image.core.getcount() + diff + yield + self.assertEqual(Image.core.getcount(), count) + def test_horizontal(self): im = hopper('L') - count = Image.core.getcount() - im.resize((im.size[0] + 10, im.size[1]), Image.BILINEAR) - self.assertEqual(Image.core.getcount(), count + 1) + with self.count(1): + im.resize((im.size[0] - 10, im.size[1]), Image.BILINEAR) def test_vertical(self): im = hopper('L') - count = Image.core.getcount() - im.resize((im.size[0], im.size[1] + 10), Image.BILINEAR) - self.assertEqual(Image.core.getcount(), count + 1) + with self.count(1): + im.resize((im.size[0], im.size[1] - 10), Image.BILINEAR) def test_both(self): im = hopper('L') - count = Image.core.getcount() - im.resize((im.size[0] + 10, im.size[1] + 10), Image.BILINEAR) - self.assertEqual(Image.core.getcount(), count + 2) + with self.count(2): + im.resize((im.size[0] - 10, im.size[1] - 10), Image.BILINEAR) + def test_box_horizontal(self): + im = hopper('L') + box = (20, 0, im.size[0] - 20, im.size[1]) + with self.count(1): + # the same size, but different box + with_box = im.resize(im.size, Image.BILINEAR, box) + cropped = im.crop(box).resize(im.size, Image.BILINEAR) + self.assert_image_similar(with_box, cropped, 0.1) + + def test_box_vertical(self): + im = hopper('L') + box = (0, 20, im.size[0], im.size[1] - 20) + with self.count(1): + # the same size, but different box + with_box = im.resize(im.size, Image.BILINEAR, box) + cropped = im.crop(box).resize(im.size, Image.BILINEAR) + self.assert_image_similar(with_box, cropped, 0.1) class CoreResampleCoefficientsTest(PillowTestCase): def test_reduce(self):