Merge pull request #2252 from uploadcare/resample-roi

Resample and tests issues
This commit is contained in:
Alexander Karpinsky 2016-11-24 04:24:57 +03:00 committed by GitHub
commit 0d08ec1c64
3 changed files with 27 additions and 18 deletions

View File

@ -1544,7 +1544,7 @@ class Image(object):
size = tuple(size) size = tuple(size)
if self.size == size: if self.size == size:
return self._new(self.im) return self.copy()
if self.mode in ("1", "P"): if self.mode in ("1", "P"):
resample = NEAREST resample = NEAREST

View File

@ -24,6 +24,15 @@ class TestImagingResampleVulnerability(PillowTestCase):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
im.resize((100, -100)) im.resize((100, -100))
def test_modify_after_resizing(self):
im = hopper('RGB')
# get copy with same size
copy = im.resize(im.size)
# some in-place operation
copy.paste('black', (0, 0, im.width // 2, im.height // 2))
# image should be different
self.assertNotEqual(im.tobytes(), copy.tobytes())
class TestImagingCoreResampleAccuracy(PillowTestCase): class TestImagingCoreResampleAccuracy(PillowTestCase):
def make_case(self, mode, size, color): def make_case(self, mode, size, color):
@ -255,7 +264,7 @@ class CoreResampleAlphaCorrectTest(PillowTestCase):
self.run_levels_case(case.resize((512, 32), Image.BICUBIC)) self.run_levels_case(case.resize((512, 32), Image.BICUBIC))
self.run_levels_case(case.resize((512, 32), Image.LANCZOS)) self.run_levels_case(case.resize((512, 32), Image.LANCZOS))
def make_dity_case(self, mode, clean_pixel, dirty_pixel): def make_dirty_case(self, mode, clean_pixel, dirty_pixel):
i = Image.new(mode, (64, 64), dirty_pixel) i = Image.new(mode, (64, 64), dirty_pixel)
px = i.load() px = i.load()
xdiv4 = i.size[0] // 4 xdiv4 = i.size[0] // 4
@ -265,7 +274,7 @@ class CoreResampleAlphaCorrectTest(PillowTestCase):
px[x + xdiv4, y + ydiv4] = clean_pixel px[x + xdiv4, y + ydiv4] = clean_pixel
return i return i
def run_dity_case(self, i, clean_pixel): def run_dirty_case(self, i, clean_pixel):
px = i.load() px = i.load()
for y in range(i.size[1]): for y in range(i.size[1]):
for x in range(i.size[0]): for x in range(i.size[0]):
@ -275,20 +284,20 @@ class CoreResampleAlphaCorrectTest(PillowTestCase):
self.assertEqual(px[x, y][:3], clean_pixel, message) self.assertEqual(px[x, y][:3], clean_pixel, message)
def test_dirty_pixels_rgba(self): def test_dirty_pixels_rgba(self):
case = self.make_dity_case('RGBA', (255, 255, 0, 128), (0, 0, 255, 0)) case = self.make_dirty_case('RGBA', (255, 255, 0, 128), (0, 0, 255, 0))
self.run_dity_case(case.resize((20, 20), Image.BOX), (255, 255, 0)) self.run_dirty_case(case.resize((20, 20), Image.BOX), (255, 255, 0))
self.run_dity_case(case.resize((20, 20), Image.BILINEAR), (255, 255, 0)) self.run_dirty_case(case.resize((20, 20), Image.BILINEAR), (255, 255, 0))
self.run_dity_case(case.resize((20, 20), Image.HAMMING), (255, 255, 0)) self.run_dirty_case(case.resize((20, 20), Image.HAMMING), (255, 255, 0))
self.run_dity_case(case.resize((20, 20), Image.BICUBIC), (255, 255, 0)) self.run_dirty_case(case.resize((20, 20), Image.BICUBIC), (255, 255, 0))
self.run_dity_case(case.resize((20, 20), Image.LANCZOS), (255, 255, 0)) self.run_dirty_case(case.resize((20, 20), Image.LANCZOS), (255, 255, 0))
def test_dirty_pixels_la(self): def test_dirty_pixels_la(self):
case = self.make_dity_case('LA', (255, 128), (0, 0)) case = self.make_dirty_case('LA', (255, 128), (0, 0))
self.run_dity_case(case.resize((20, 20), Image.BOX), (255,)) self.run_dirty_case(case.resize((20, 20), Image.BOX), (255,))
self.run_dity_case(case.resize((20, 20), Image.BILINEAR), (255,)) self.run_dirty_case(case.resize((20, 20), Image.BILINEAR), (255,))
self.run_dity_case(case.resize((20, 20), Image.HAMMING), (255,)) self.run_dirty_case(case.resize((20, 20), Image.HAMMING), (255,))
self.run_dity_case(case.resize((20, 20), Image.BICUBIC), (255,)) self.run_dirty_case(case.resize((20, 20), Image.BICUBIC), (255,))
self.run_dity_case(case.resize((20, 20), Image.LANCZOS), (255,)) self.run_dirty_case(case.resize((20, 20), Image.LANCZOS), (255,))
class CoreResamplePassesTest(PillowTestCase): class CoreResamplePassesTest(PillowTestCase):

View File

@ -39,14 +39,14 @@ class TestImagingCoreResize(PillowTestCase):
self.assertEqual(r.im.bands, im.im.bands) self.assertEqual(r.im.bands, im.im.bands)
def test_reduce_filters(self): def test_reduce_filters(self):
for f in [Image.LINEAR, Image.BOX, Image.BILINEAR, Image.HAMMING, for f in [Image.NEAREST, Image.BOX, Image.BILINEAR, Image.HAMMING,
Image.BICUBIC, Image.LANCZOS]: Image.BICUBIC, Image.LANCZOS]:
r = self.resize(hopper("RGB"), (15, 12), f) r = self.resize(hopper("RGB"), (15, 12), f)
self.assertEqual(r.mode, "RGB") self.assertEqual(r.mode, "RGB")
self.assertEqual(r.size, (15, 12)) self.assertEqual(r.size, (15, 12))
def test_enlarge_filters(self): def test_enlarge_filters(self):
for f in [Image.LINEAR, Image.BOX, Image.BILINEAR, Image.HAMMING, for f in [Image.NEAREST, Image.BOX, Image.BILINEAR, Image.HAMMING,
Image.BICUBIC, Image.LANCZOS]: Image.BICUBIC, Image.LANCZOS]:
r = self.resize(hopper("RGB"), (212, 195), f) r = self.resize(hopper("RGB"), (212, 195), f)
self.assertEqual(r.mode, "RGB") self.assertEqual(r.mode, "RGB")
@ -66,7 +66,7 @@ class TestImagingCoreResize(PillowTestCase):
} }
samples['dirty'].putpixel((1, 1), 128) samples['dirty'].putpixel((1, 1), 128)
for f in [Image.LINEAR, Image.BOX, Image.BILINEAR, Image.HAMMING, for f in [Image.NEAREST, Image.BOX, Image.BILINEAR, Image.HAMMING,
Image.BICUBIC, Image.LANCZOS]: Image.BICUBIC, Image.LANCZOS]:
# samples resized with current filter # samples resized with current filter
references = { references = {