mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-25 17:36:18 +03:00
merge imaging and imaging core resize tests
This commit is contained in:
parent
513e2a12dd
commit
24ed800fae
|
@ -1,5 +1,91 @@
|
|||
"""
|
||||
Tests for resize functionality.
|
||||
"""
|
||||
from itertools import permutations
|
||||
|
||||
from helper import unittest, PillowTestCase, hopper
|
||||
|
||||
from PIL import Image
|
||||
|
||||
|
||||
class TestImagingCoreResize(PillowTestCase):
|
||||
|
||||
def resize(self, im, size, f):
|
||||
# Image class independend version of resize.
|
||||
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",
|
||||
"I;16"]: # exotic mode
|
||||
im = hopper(mode)
|
||||
r = self.resize(im, (15, 12), Image.NEAREST)
|
||||
self.assertEqual(r.mode, mode)
|
||||
self.assertEqual(r.size, (15, 12) )
|
||||
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)
|
||||
self.assertEqual(r.size, (15, 12) )
|
||||
self.assertEqual(r.im.bands, im.im.bands)
|
||||
|
||||
def test_reduce_filters(self):
|
||||
for f in [Image.LINEAR, Image.BILINEAR, Image.BICUBIC, Image.ANTIALIAS]:
|
||||
r = self.resize(hopper("RGB"), (15, 12), f)
|
||||
self.assertEqual(r.mode, "RGB")
|
||||
self.assertEqual(r.size, (15, 12))
|
||||
|
||||
def test_enlarge_filters(self):
|
||||
for f in [Image.LINEAR, Image.BILINEAR, Image.BICUBIC, Image.ANTIALIAS]:
|
||||
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.
|
||||
# The R and A channels should not swap, which is indicitive of
|
||||
# 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)
|
||||
|
||||
for f in [Image.LINEAR, Image.BILINEAR, Image.BICUBIC, Image.ANTIALIAS]:
|
||||
# samples resized with current filter
|
||||
references = dict(
|
||||
(name, self.resize(ch, (4, 4), f))
|
||||
for name, ch in samples.items()
|
||||
)
|
||||
|
||||
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]])
|
||||
|
||||
|
||||
class TestImageResize(PillowTestCase):
|
||||
|
||||
|
@ -9,8 +95,8 @@ class TestImageResize(PillowTestCase):
|
|||
self.assertEqual(out.mode, mode)
|
||||
self.assertEqual(out.size, size)
|
||||
for mode in "1", "P", "L", "RGB", "I", "F":
|
||||
resize(mode, (100, 100))
|
||||
resize(mode, (200, 200))
|
||||
resize(mode, (112, 103))
|
||||
resize(mode, (188, 214))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
@ -1,93 +0,0 @@
|
|||
"""
|
||||
Tests for resize functionality.
|
||||
"""
|
||||
from itertools import permutations
|
||||
|
||||
from helper import unittest, PillowTestCase, hopper
|
||||
|
||||
from PIL import Image
|
||||
|
||||
|
||||
class TestImagingCoreResize(PillowTestCase):
|
||||
|
||||
def resize(self, im, size, f):
|
||||
# Image class independend version of resize.
|
||||
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",
|
||||
"I;16"]: # exotic mode
|
||||
im = hopper(mode)
|
||||
r = self.resize(im, (15, 12), Image.NEAREST)
|
||||
self.assertEqual(r.mode, mode)
|
||||
self.assertEqual(r.size, (15, 12) )
|
||||
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)
|
||||
self.assertEqual(r.size, (15, 12) )
|
||||
self.assertEqual(r.im.bands, im.im.bands)
|
||||
|
||||
def test_reduce_filters(self):
|
||||
for f in [Image.LINEAR, Image.BILINEAR, Image.BICUBIC, Image.ANTIALIAS]:
|
||||
r = self.resize(hopper("RGB"), (15, 12), f)
|
||||
self.assertEqual(r.mode, "RGB")
|
||||
self.assertEqual(r.size, (15, 12))
|
||||
|
||||
def test_enlarge_filters(self):
|
||||
for f in [Image.LINEAR, Image.BILINEAR, Image.BICUBIC, Image.ANTIALIAS]:
|
||||
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.
|
||||
# The R and A channels should not swap, which is indicitive of
|
||||
# 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)
|
||||
|
||||
for f in [Image.LINEAR, Image.BILINEAR, Image.BICUBIC, Image.ANTIALIAS]:
|
||||
# samples resized with current filter
|
||||
references = dict(
|
||||
(name, self.resize(ch, (4, 4), f))
|
||||
for name, ch in samples.items()
|
||||
)
|
||||
|
||||
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]])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
# End of file
|
Loading…
Reference in New Issue
Block a user