2014-11-19 03:05:39 +03:00
|
|
|
"""
|
|
|
|
Tests for resize functionality.
|
|
|
|
"""
|
|
|
|
from itertools import permutations
|
|
|
|
|
2019-01-13 20:00:12 +03:00
|
|
|
from .helper import unittest, PillowTestCase, hopper
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2014-11-19 03:05:39 +03:00
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
class TestImagingCoreResize(PillowTestCase):
|
|
|
|
|
|
|
|
def resize(self, im, size, f):
|
2014-11-19 11:26:02 +03:00
|
|
|
# Image class independent version of resize.
|
2014-11-19 03:05:39 +03:00
|
|
|
im.load()
|
|
|
|
return im._new(im.im.resize(size, f))
|
|
|
|
|
|
|
|
def test_nearest_mode(self):
|
|
|
|
for mode in ["1", "P", "L", "I", "F", "RGB", "RGBA", "CMYK", "YCbCr",
|
2015-04-24 02:26:52 +03:00
|
|
|
"I;16"]: # exotic mode
|
2014-11-19 03:05:39 +03:00
|
|
|
im = hopper(mode)
|
|
|
|
r = self.resize(im, (15, 12), Image.NEAREST)
|
|
|
|
self.assertEqual(r.mode, mode)
|
2015-04-24 02:26:52 +03:00
|
|
|
self.assertEqual(r.size, (15, 12))
|
2014-11-19 03:05:39 +03:00
|
|
|
self.assertEqual(r.im.bands, im.im.bands)
|
|
|
|
|
|
|
|
def test_convolution_modes(self):
|
|
|
|
self.assertRaises(ValueError, self.resize, hopper("1"),
|
|
|
|
(15, 12), Image.BILINEAR)
|
|
|
|
self.assertRaises(ValueError, self.resize, hopper("P"),
|
|
|
|
(15, 12), Image.BILINEAR)
|
|
|
|
self.assertRaises(ValueError, self.resize, hopper("I;16"),
|
|
|
|
(15, 12), Image.BILINEAR)
|
|
|
|
for mode in ["L", "I", "F", "RGB", "RGBA", "CMYK", "YCbCr"]:
|
|
|
|
im = hopper(mode)
|
|
|
|
r = self.resize(im, (15, 12), Image.BILINEAR)
|
|
|
|
self.assertEqual(r.mode, mode)
|
2015-04-24 02:26:52 +03:00
|
|
|
self.assertEqual(r.size, (15, 12))
|
2014-11-19 03:05:39 +03:00
|
|
|
self.assertEqual(r.im.bands, im.im.bands)
|
|
|
|
|
|
|
|
def test_reduce_filters(self):
|
2018-06-24 14:55:22 +03:00
|
|
|
for f in [Image.NEAREST, Image.BOX, Image.BILINEAR,
|
|
|
|
Image.HAMMING, Image.BICUBIC, Image.LANCZOS]:
|
2014-11-19 03:05:39 +03:00
|
|
|
r = self.resize(hopper("RGB"), (15, 12), f)
|
|
|
|
self.assertEqual(r.mode, "RGB")
|
|
|
|
self.assertEqual(r.size, (15, 12))
|
|
|
|
|
|
|
|
def test_enlarge_filters(self):
|
2018-06-24 14:55:22 +03:00
|
|
|
for f in [Image.NEAREST, Image.BOX, Image.BILINEAR,
|
|
|
|
Image.HAMMING, Image.BICUBIC, Image.LANCZOS]:
|
2014-11-19 03:05:39 +03:00
|
|
|
r = self.resize(hopper("RGB"), (212, 195), f)
|
|
|
|
self.assertEqual(r.mode, "RGB")
|
|
|
|
self.assertEqual(r.size, (212, 195))
|
|
|
|
|
|
|
|
def test_endianness(self):
|
|
|
|
# Make an image with one colored pixel, in one channel.
|
|
|
|
# When resized, that channel should be the same as a GS image.
|
|
|
|
# Other channels should be unaffected.
|
2015-05-29 07:59:54 +03:00
|
|
|
# The R and A channels should not swap, which is indicative of
|
2014-11-19 03:05:39 +03:00
|
|
|
# an endianness issues.
|
|
|
|
|
|
|
|
samples = {
|
|
|
|
'blank': Image.new('L', (2, 2), 0),
|
|
|
|
'filled': Image.new('L', (2, 2), 255),
|
|
|
|
'dirty': Image.new('L', (2, 2), 0),
|
|
|
|
}
|
|
|
|
samples['dirty'].putpixel((1, 1), 128)
|
|
|
|
|
2018-06-24 14:55:22 +03:00
|
|
|
for f in [Image.NEAREST, Image.BOX, Image.BILINEAR,
|
|
|
|
Image.HAMMING, Image.BICUBIC, Image.LANCZOS]:
|
2014-11-19 03:05:39 +03:00
|
|
|
# samples resized with current filter
|
2016-11-07 15:33:46 +03:00
|
|
|
references = {
|
|
|
|
name: self.resize(ch, (4, 4), f)
|
2014-11-19 03:05:39 +03:00
|
|
|
for name, ch in samples.items()
|
2016-11-07 15:33:46 +03:00
|
|
|
}
|
2014-11-19 03:05:39 +03:00
|
|
|
|
|
|
|
for mode, channels_set in [
|
|
|
|
('RGB', ('blank', 'filled', 'dirty')),
|
|
|
|
('RGBA', ('blank', 'blank', 'filled', 'dirty')),
|
|
|
|
('LA', ('filled', 'dirty')),
|
|
|
|
]:
|
|
|
|
for channels in set(permutations(channels_set)):
|
|
|
|
# compile image from different channels permutations
|
|
|
|
im = Image.merge(mode, [samples[ch] for ch in channels])
|
|
|
|
resized = self.resize(im, (4, 4), f)
|
|
|
|
|
|
|
|
for i, ch in enumerate(resized.split()):
|
|
|
|
# check what resized channel in image is the same
|
|
|
|
# as separately resized channel
|
|
|
|
self.assert_image_equal(ch, references[channels[i]])
|
|
|
|
|
2016-12-27 15:53:23 +03:00
|
|
|
def test_enlarge_zero(self):
|
2018-06-24 14:55:22 +03:00
|
|
|
for f in [Image.NEAREST, Image.BOX, Image.BILINEAR,
|
|
|
|
Image.HAMMING, Image.BICUBIC, Image.LANCZOS]:
|
2017-04-20 14:14:23 +03:00
|
|
|
r = self.resize(Image.new('RGB', (0, 0), "white"), (212, 195), f)
|
2016-12-27 15:53:23 +03:00
|
|
|
self.assertEqual(r.mode, "RGB")
|
|
|
|
self.assertEqual(r.size, (212, 195))
|
2017-04-20 14:14:23 +03:00
|
|
|
self.assertEqual(r.getdata()[0], (0, 0, 0))
|
2016-12-27 15:53:23 +03:00
|
|
|
|
2017-01-28 06:09:28 +03:00
|
|
|
def test_unknown_filter(self):
|
|
|
|
self.assertRaises(ValueError, self.resize, hopper(), (10, 10), 9)
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
class TestImageResize(PillowTestCase):
|
|
|
|
|
|
|
|
def test_resize(self):
|
|
|
|
def resize(mode, size):
|
2014-09-05 14:03:56 +04:00
|
|
|
out = hopper(mode).resize(size)
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assertEqual(out.mode, mode)
|
|
|
|
self.assertEqual(out.size, size)
|
|
|
|
for mode in "1", "P", "L", "RGB", "I", "F":
|
2014-11-19 03:05:39 +03:00
|
|
|
resize(mode, (112, 103))
|
|
|
|
resize(mode, (188, 214))
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2017-09-01 13:36:51 +03:00
|
|
|
# Test unknown resampling filter
|
|
|
|
im = hopper()
|
2017-09-01 14:05:40 +03:00
|
|
|
self.assertRaises(ValueError, im.resize, (10, 10), "unknown")
|
2017-09-01 13:36:51 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|