resize test rework

This commit is contained in:
homm 2014-11-19 03:03:49 +03:00
parent aabf66a7a4
commit 513e2a12dd

View File

@ -1,51 +1,58 @@
""" """
Tests for ImagingCore.stretch functionality. Tests for resize functionality.
""" """
from itertools import permutations from itertools import permutations
from helper import unittest, PillowTestCase from helper import unittest, PillowTestCase, hopper
from PIL import Image from PIL import Image
im = Image.open("Tests/images/hopper.ppm").copy() 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))
class TestImagingStretch(PillowTestCase): def test_nearest_mode(self):
for mode in ["1", "P", "L", "I", "F", "RGB", "RGBA", "CMYK", "YCbCr",
def stretch(self, im, size, f): "I;16"]: # exotic mode
return im._new(im.im.stretch(size, f)) im = hopper(mode)
r = self.resize(im, (15, 12), Image.NEAREST)
def test_modes(self):
self.assertRaises(ValueError, im.convert("1").im.stretch,
(15, 12), Image.ANTIALIAS)
self.assertRaises(ValueError, im.convert("P").im.stretch,
(15, 12), Image.ANTIALIAS)
for mode in ["L", "I", "F", "RGB", "RGBA", "CMYK", "YCbCr"]:
s = im.convert(mode).im
r = s.stretch((15, 12), Image.ANTIALIAS)
self.assertEqual(r.mode, mode) self.assertEqual(r.mode, mode)
self.assertEqual(r.size, (15, 12)) self.assertEqual(r.size, (15, 12) )
self.assertEqual(r.bands, s.bands) 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): def test_reduce_filters(self):
# There is no Image.NEAREST because im.stretch implementation for f in [Image.LINEAR, Image.BILINEAR, Image.BICUBIC, Image.ANTIALIAS]:
# is not NEAREST for reduction. It should be removed r = self.resize(hopper("RGB"), (15, 12), f)
# or renamed to supersampling.
for f in [Image.BILINEAR, Image.BICUBIC, Image.ANTIALIAS]:
r = im.im.stretch((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.BILINEAR, Image.BICUBIC, Image.ANTIALIAS]: for f in [Image.LINEAR, Image.BILINEAR, Image.BICUBIC, Image.ANTIALIAS]:
r = im.im.stretch((764, 414), f) r = self.resize(hopper("RGB"), (212, 195), f)
self.assertEqual(r.mode, "RGB") self.assertEqual(r.mode, "RGB")
self.assertEqual(r.size, (764, 414)) self.assertEqual(r.size, (212, 195))
def test_endianness(self): def test_endianness(self):
# Make an image with one colored pixel, in one channel. # Make an image with one colored pixel, in one channel.
# When stretched, that channel should be the same as a GS image. # When resized, that channel should be the same as a GS image.
# Other channels should be unaffected. # Other channels should be unaffected.
# The R and A channels should not swap, which is indicitive of # The R and A channels should not swap, which is indicitive of
# an endianness issues. # an endianness issues.
@ -57,10 +64,10 @@ class TestImagingStretch(PillowTestCase):
} }
samples['dirty'].putpixel((1, 1), 128) samples['dirty'].putpixel((1, 1), 128)
for f in [Image.BILINEAR, Image.BICUBIC, Image.ANTIALIAS]: for f in [Image.LINEAR, Image.BILINEAR, Image.BICUBIC, Image.ANTIALIAS]:
# samples resized with current filter # samples resized with current filter
resized = dict( references = dict(
(name, self.stretch(ch, (4, 4), f)) (name, self.resize(ch, (4, 4), f))
for name, ch in samples.items() for name, ch in samples.items()
) )
@ -72,12 +79,12 @@ class TestImagingStretch(PillowTestCase):
for channels in set(permutations(channels_set)): for channels in set(permutations(channels_set)):
# compile image from different channels permutations # compile image from different channels permutations
im = Image.merge(mode, [samples[ch] for ch in channels]) im = Image.merge(mode, [samples[ch] for ch in channels])
stretched = self.stretch(im, (4, 4), f) resized = self.resize(im, (4, 4), f)
for i, ch in enumerate(stretched.split()): for i, ch in enumerate(resized.split()):
# check what resized channel in image is the same # check what resized channel in image is the same
# as separately resized channel # as separately resized channel
self.assert_image_equal(ch, resized[channels[i]]) self.assert_image_equal(ch, references[channels[i]])
if __name__ == '__main__': if __name__ == '__main__':