Further parametrizations

This commit is contained in:
Andrew Murray 2022-08-24 10:37:40 +10:00
parent 8f25ea46eb
commit 3353ea80e1
2 changed files with 143 additions and 130 deletions

View File

@ -514,13 +514,15 @@ class TestCoreResampleBox:
assert_image_similar(reference, without_box, 5) assert_image_similar(reference, without_box, 5)
@pytest.mark.parametrize("mode", ("RGB", "L", "RGBA", "LA", "I", "")) @pytest.mark.parametrize("mode", ("RGB", "L", "RGBA", "LA", "I", ""))
def test_formats(self, mode): @pytest.mark.parametrize(
for resample in [Image.Resampling.NEAREST, Image.Resampling.BILINEAR]: "resample", (Image.Resampling.NEAREST, Image.Resampling.BILINEAR)
im = hopper(mode) )
box = (20, 20, im.size[0] - 20, im.size[1] - 20) def test_formats(self, mode, resample):
with_box = im.resize((32, 32), resample, box) im = hopper(mode)
cropped = im.crop(box).resize((32, 32), resample) box = (20, 20, im.size[0] - 20, im.size[1] - 20)
assert_image_similar(cropped, with_box, 0.4) with_box = im.resize((32, 32), resample, box)
cropped = im.crop(box).resize((32, 32), resample)
assert_image_similar(cropped, with_box, 0.4)
def test_passthrough(self): def test_passthrough(self):
# When no resize is required # When no resize is required

View File

@ -46,33 +46,58 @@ class TestImagingCoreResize:
assert r.size == (15, 12) assert r.size == (15, 12)
assert r.im.bands == im.im.bands assert r.im.bands == im.im.bands
def test_reduce_filters(self): @pytest.mark.parametrize(
for f in [ "resample",
(
Image.Resampling.NEAREST, Image.Resampling.NEAREST,
Image.Resampling.BOX, Image.Resampling.BOX,
Image.Resampling.BILINEAR, Image.Resampling.BILINEAR,
Image.Resampling.HAMMING, Image.Resampling.HAMMING,
Image.Resampling.BICUBIC, Image.Resampling.BICUBIC,
Image.Resampling.LANCZOS, Image.Resampling.LANCZOS,
]: ),
r = self.resize(hopper("RGB"), (15, 12), f) )
assert r.mode == "RGB" def test_reduce_filters(self, resample):
assert r.size == (15, 12) r = self.resize(hopper("RGB"), (15, 12), resample)
assert r.mode == "RGB"
assert r.size == (15, 12)
def test_enlarge_filters(self): @pytest.mark.parametrize(
for f in [ "resample",
(
Image.Resampling.NEAREST, Image.Resampling.NEAREST,
Image.Resampling.BOX, Image.Resampling.BOX,
Image.Resampling.BILINEAR, Image.Resampling.BILINEAR,
Image.Resampling.HAMMING, Image.Resampling.HAMMING,
Image.Resampling.BICUBIC, Image.Resampling.BICUBIC,
Image.Resampling.LANCZOS, Image.Resampling.LANCZOS,
]: ),
r = self.resize(hopper("RGB"), (212, 195), f) )
assert r.mode == "RGB" def test_enlarge_filters(self, resample):
assert r.size == (212, 195) r = self.resize(hopper("RGB"), (212, 195), resample)
assert r.mode == "RGB"
assert r.size == (212, 195)
def test_endianness(self): @pytest.mark.parametrize(
"resample",
(
Image.Resampling.NEAREST,
Image.Resampling.BOX,
Image.Resampling.BILINEAR,
Image.Resampling.HAMMING,
Image.Resampling.BICUBIC,
Image.Resampling.LANCZOS,
),
)
@pytest.mark.parametrize(
"mode, channels_set",
(
("RGB", ("blank", "filled", "dirty")),
("RGBA", ("blank", "blank", "filled", "dirty")),
("LA", ("filled", "dirty")),
),
)
def test_endianness(self, resample, mode, channels_set):
# Make an image with one colored pixel, in one channel. # Make an image with one colored pixel, in one channel.
# When resized, 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.
@ -86,47 +111,37 @@ class TestImagingCoreResize:
} }
samples["dirty"].putpixel((1, 1), 128) samples["dirty"].putpixel((1, 1), 128)
for f in [ # samples resized with current filter
references = {
name: self.resize(ch, (4, 4), resample) for name, ch in samples.items()
}
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), resample)
for i, ch in enumerate(resized.split()):
# check what resized channel in image is the same
# as separately resized channel
assert_image_equal(ch, references[channels[i]])
@pytest.mark.parametrize(
"resample",
(
Image.Resampling.NEAREST, Image.Resampling.NEAREST,
Image.Resampling.BOX, Image.Resampling.BOX,
Image.Resampling.BILINEAR, Image.Resampling.BILINEAR,
Image.Resampling.HAMMING, Image.Resampling.HAMMING,
Image.Resampling.BICUBIC, Image.Resampling.BICUBIC,
Image.Resampling.LANCZOS, Image.Resampling.LANCZOS,
]: ),
# samples resized with current filter )
references = { def test_enlarge_zero(self, resample):
name: self.resize(ch, (4, 4), f) for name, ch in samples.items() r = self.resize(Image.new("RGB", (0, 0), "white"), (212, 195), resample)
} assert r.mode == "RGB"
assert r.size == (212, 195)
for mode, channels_set in [ assert r.getdata()[0] == (0, 0, 0)
("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
assert_image_equal(ch, references[channels[i]])
def test_enlarge_zero(self):
for f in [
Image.Resampling.NEAREST,
Image.Resampling.BOX,
Image.Resampling.BILINEAR,
Image.Resampling.HAMMING,
Image.Resampling.BICUBIC,
Image.Resampling.LANCZOS,
]:
r = self.resize(Image.new("RGB", (0, 0), "white"), (212, 195), f)
assert r.mode == "RGB"
assert r.size == (212, 195)
assert r.getdata()[0] == (0, 0, 0)
def test_unknown_filter(self): def test_unknown_filter(self):
with pytest.raises(ValueError): with pytest.raises(ValueError):
@ -170,74 +185,71 @@ class TestReducingGapResize:
(52, 34), Image.Resampling.BICUBIC, reducing_gap=0.99 (52, 34), Image.Resampling.BICUBIC, reducing_gap=0.99
) )
def test_reducing_gap_1(self, gradients_image): @pytest.mark.parametrize(
for box, epsilon in [ "box, epsilon",
(None, 4), ((None, 4), ((1.1, 2.2, 510.8, 510.9), 4), ((3, 10, 410, 256), 10)),
((1.1, 2.2, 510.8, 510.9), 4), )
((3, 10, 410, 256), 10), def test_reducing_gap_1(self, gradients_image, box, epsilon):
]: ref = gradients_image.resize((52, 34), Image.Resampling.BICUBIC, box=box)
ref = gradients_image.resize((52, 34), Image.Resampling.BICUBIC, box=box) im = gradients_image.resize(
im = gradients_image.resize( (52, 34), Image.Resampling.BICUBIC, box=box, reducing_gap=1.0
(52, 34), Image.Resampling.BICUBIC, box=box, reducing_gap=1.0 )
)
with pytest.raises(AssertionError):
assert_image_equal(ref, im)
assert_image_similar(ref, im, epsilon)
def test_reducing_gap_2(self, gradients_image):
for box, epsilon in [
(None, 1.5),
((1.1, 2.2, 510.8, 510.9), 1.5),
((3, 10, 410, 256), 1),
]:
ref = gradients_image.resize((52, 34), Image.Resampling.BICUBIC, box=box)
im = gradients_image.resize(
(52, 34), Image.Resampling.BICUBIC, box=box, reducing_gap=2.0
)
with pytest.raises(AssertionError):
assert_image_equal(ref, im)
assert_image_similar(ref, im, epsilon)
def test_reducing_gap_3(self, gradients_image):
for box, epsilon in [
(None, 1),
((1.1, 2.2, 510.8, 510.9), 1),
((3, 10, 410, 256), 0.5),
]:
ref = gradients_image.resize((52, 34), Image.Resampling.BICUBIC, box=box)
im = gradients_image.resize(
(52, 34), Image.Resampling.BICUBIC, box=box, reducing_gap=3.0
)
with pytest.raises(AssertionError):
assert_image_equal(ref, im)
assert_image_similar(ref, im, epsilon)
def test_reducing_gap_8(self, gradients_image):
for box in [None, (1.1, 2.2, 510.8, 510.9), (3, 10, 410, 256)]:
ref = gradients_image.resize((52, 34), Image.Resampling.BICUBIC, box=box)
im = gradients_image.resize(
(52, 34), Image.Resampling.BICUBIC, box=box, reducing_gap=8.0
)
with pytest.raises(AssertionError):
assert_image_equal(ref, im) assert_image_equal(ref, im)
def test_box_filter(self, gradients_image): assert_image_similar(ref, im, epsilon)
for box, epsilon in [
((0, 0, 512, 512), 5.5),
((0.9, 1.7, 128, 128), 9.5),
]:
ref = gradients_image.resize((52, 34), Image.Resampling.BOX, box=box)
im = gradients_image.resize(
(52, 34), Image.Resampling.BOX, box=box, reducing_gap=1.0
)
assert_image_similar(ref, im, epsilon) @pytest.mark.parametrize(
"box, epsilon",
((None, 1.5), ((1.1, 2.2, 510.8, 510.9), 1.5), ((3, 10, 410, 256), 1)),
)
def test_reducing_gap_2(self, gradients_image, box, epsilon):
ref = gradients_image.resize((52, 34), Image.Resampling.BICUBIC, box=box)
im = gradients_image.resize(
(52, 34), Image.Resampling.BICUBIC, box=box, reducing_gap=2.0
)
with pytest.raises(AssertionError):
assert_image_equal(ref, im)
assert_image_similar(ref, im, epsilon)
@pytest.mark.parametrize(
"box, epsilon",
((None, 1), ((1.1, 2.2, 510.8, 510.9), 1), ((3, 10, 410, 256), 0.5)),
)
def test_reducing_gap_3(self, gradients_image, box, epsilon):
ref = gradients_image.resize((52, 34), Image.Resampling.BICUBIC, box=box)
im = gradients_image.resize(
(52, 34), Image.Resampling.BICUBIC, box=box, reducing_gap=3.0
)
with pytest.raises(AssertionError):
assert_image_equal(ref, im)
assert_image_similar(ref, im, epsilon)
@pytest.mark.parametrize("box", (None, (1.1, 2.2, 510.8, 510.9), (3, 10, 410, 256)))
def test_reducing_gap_8(self, gradients_image, box):
ref = gradients_image.resize((52, 34), Image.Resampling.BICUBIC, box=box)
im = gradients_image.resize(
(52, 34), Image.Resampling.BICUBIC, box=box, reducing_gap=8.0
)
assert_image_equal(ref, im)
@pytest.mark.parametrize(
"box, epsilon",
(((0, 0, 512, 512), 5.5), ((0.9, 1.7, 128, 128), 9.5)),
)
def test_box_filter(self, gradients_image, box, epsilon):
ref = gradients_image.resize((52, 34), Image.Resampling.BOX, box=box)
im = gradients_image.resize(
(52, 34), Image.Resampling.BOX, box=box, reducing_gap=1.0
)
assert_image_similar(ref, im, epsilon)
class TestImageResize: class TestImageResize:
@ -264,15 +276,14 @@ class TestImageResize:
im = im.resize((64, 64)) im = im.resize((64, 64))
assert im.size == (64, 64) assert im.size == (64, 64)
def test_default_filter(self): @pytest.mark.parametrize("mode", ("L", "RGB", "I", "F"))
for mode in "L", "RGB", "I", "F": def test_default_filter_bicubic(self, mode):
im = hopper(mode) im = hopper(mode)
assert im.resize((20, 20), Image.Resampling.BICUBIC) == im.resize((20, 20)) assert im.resize((20, 20), Image.Resampling.BICUBIC) == im.resize((20, 20))
for mode in "1", "P": @pytest.mark.parametrize(
im = hopper(mode) "mode", ("1", "P", "I;16", "I;16L", "I;16B", "BGR;15", "BGR;16")
assert im.resize((20, 20), Image.Resampling.NEAREST) == im.resize((20, 20)) )
def test_default_filter_nearest(self, mode):
for mode in "I;16", "I;16L", "I;16B", "BGR;15", "BGR;16": im = hopper(mode)
im = hopper(mode) assert im.resize((20, 20), Image.Resampling.NEAREST) == im.resize((20, 20))
assert im.resize((20, 20), Image.Resampling.NEAREST) == im.resize((20, 20))