mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-13 10:46:16 +03:00
vertical and horizontal pass with boxes
This commit is contained in:
parent
3d72e9828f
commit
295382aadc
13
PIL/Image.py
13
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))
|
||||
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue
Block a user