vertical and horizontal pass with boxes

This commit is contained in:
homm 2016-12-02 15:40:32 +03:00
parent 3d72e9828f
commit 295382aadc
2 changed files with 39 additions and 14 deletions

View File

@ -1544,10 +1544,14 @@ class Image(object):
): ):
raise ValueError("unknown resampling filter") raise ValueError("unknown resampling filter")
self.load()
size = tuple(size) 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() return self.copy()
if self.mode in ("1", "P"): if self.mode in ("1", "P"):
@ -1559,8 +1563,7 @@ class Image(object):
if self.mode == 'RGBA': if self.mode == 'RGBA':
return self.convert('RGBa').resize(size, resample).convert('RGBA') return self.convert('RGBa').resize(size, resample).convert('RGBA')
if box is None: self.load()
box = (0, 0) + self.size
return self._new(self.im.resize(size, resample, box)) return self._new(self.im.resize(size, resample, box))

View File

@ -1,5 +1,7 @@
from __future__ import division from __future__ import division
from contextlib import contextmanager
from helper import unittest, PillowTestCase, hopper from helper import unittest, PillowTestCase, hopper
from PIL import Image, ImageDraw, ImageMode from PIL import Image, ImageDraw, ImageMode
@ -303,24 +305,44 @@ class CoreResampleAlphaCorrectTest(PillowTestCase):
class CoreResamplePassesTest(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): def test_horizontal(self):
im = hopper('L') im = hopper('L')
count = Image.core.getcount() with self.count(1):
im.resize((im.size[0] + 10, im.size[1]), Image.BILINEAR) im.resize((im.size[0] - 10, im.size[1]), Image.BILINEAR)
self.assertEqual(Image.core.getcount(), count + 1)
def test_vertical(self): def test_vertical(self):
im = hopper('L') im = hopper('L')
count = Image.core.getcount() with self.count(1):
im.resize((im.size[0], im.size[1] + 10), Image.BILINEAR) im.resize((im.size[0], im.size[1] - 10), Image.BILINEAR)
self.assertEqual(Image.core.getcount(), count + 1)
def test_both(self): def test_both(self):
im = hopper('L') im = hopper('L')
count = Image.core.getcount() with self.count(2):
im.resize((im.size[0] + 10, im.size[1] + 10), Image.BILINEAR) im.resize((im.size[0] - 10, im.size[1] - 10), Image.BILINEAR)
self.assertEqual(Image.core.getcount(), count + 2)
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): class CoreResampleCoefficientsTest(PillowTestCase):
def test_reduce(self): def test_reduce(self):