2014-11-19 03:05:39 +03:00
|
|
|
"""
|
|
|
|
Tests for resize functionality.
|
|
|
|
"""
|
|
|
|
from itertools import permutations
|
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
import pytest
|
2020-08-07 13:28:33 +03:00
|
|
|
|
2014-11-19 03:05:39 +03:00
|
|
|
from PIL import Image
|
|
|
|
|
2020-03-28 04:51:28 +03:00
|
|
|
from .helper import assert_image_equal, assert_image_similar, hopper
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2014-11-19 03:05:39 +03:00
|
|
|
|
2020-03-28 04:51:28 +03:00
|
|
|
class TestImagingCoreResize:
|
2014-11-19 03:05:39 +03:00
|
|
|
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):
|
2019-06-13 18:54:24 +03:00
|
|
|
for mode in [
|
|
|
|
"1",
|
|
|
|
"P",
|
|
|
|
"L",
|
|
|
|
"I",
|
|
|
|
"F",
|
|
|
|
"RGB",
|
|
|
|
"RGBA",
|
|
|
|
"CMYK",
|
|
|
|
"YCbCr",
|
|
|
|
"I;16",
|
|
|
|
]: # exotic mode
|
2014-11-19 03:05:39 +03:00
|
|
|
im = hopper(mode)
|
|
|
|
r = self.resize(im, (15, 12), Image.NEAREST)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert r.mode == mode
|
|
|
|
assert r.size == (15, 12)
|
|
|
|
assert r.im.bands == im.im.bands
|
2014-11-19 03:05:39 +03:00
|
|
|
|
|
|
|
def test_convolution_modes(self):
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
self.resize(hopper("1"), (15, 12), Image.BILINEAR)
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
self.resize(hopper("P"), (15, 12), Image.BILINEAR)
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
self.resize(hopper("I;16"), (15, 12), Image.BILINEAR)
|
2014-11-19 03:05:39 +03:00
|
|
|
for mode in ["L", "I", "F", "RGB", "RGBA", "CMYK", "YCbCr"]:
|
|
|
|
im = hopper(mode)
|
|
|
|
r = self.resize(im, (15, 12), Image.BILINEAR)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert r.mode == mode
|
|
|
|
assert r.size == (15, 12)
|
|
|
|
assert r.im.bands == im.im.bands
|
2014-11-19 03:05:39 +03:00
|
|
|
|
|
|
|
def test_reduce_filters(self):
|
2019-06-13 18:54:24 +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)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert r.mode == "RGB"
|
|
|
|
assert r.size == (15, 12)
|
2014-11-19 03:05:39 +03:00
|
|
|
|
|
|
|
def test_enlarge_filters(self):
|
2019-06-13 18:54:24 +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)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert r.mode == "RGB"
|
|
|
|
assert r.size == (212, 195)
|
2014-11-19 03:05:39 +03:00
|
|
|
|
|
|
|
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 = {
|
2019-06-13 18:54:24 +03:00
|
|
|
"blank": Image.new("L", (2, 2), 0),
|
|
|
|
"filled": Image.new("L", (2, 2), 255),
|
|
|
|
"dirty": Image.new("L", (2, 2), 0),
|
2014-11-19 03:05:39 +03:00
|
|
|
}
|
2019-06-13 18:54:24 +03:00
|
|
|
samples["dirty"].putpixel((1, 1), 128)
|
|
|
|
|
|
|
|
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 = {
|
2019-06-13 18:54:24 +03:00
|
|
|
name: self.resize(ch, (4, 4), f) 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 [
|
2019-06-13 18:54:24 +03:00
|
|
|
("RGB", ("blank", "filled", "dirty")),
|
|
|
|
("RGBA", ("blank", "blank", "filled", "dirty")),
|
|
|
|
("LA", ("filled", "dirty")),
|
2014-11-19 03:05:39 +03:00
|
|
|
]:
|
|
|
|
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
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(ch, references[channels[i]])
|
2014-11-19 03:05:39 +03:00
|
|
|
|
2016-12-27 15:53:23 +03:00
|
|
|
def test_enlarge_zero(self):
|
2019-06-13 18:54:24 +03:00
|
|
|
for f in [
|
|
|
|
Image.NEAREST,
|
|
|
|
Image.BOX,
|
|
|
|
Image.BILINEAR,
|
|
|
|
Image.HAMMING,
|
|
|
|
Image.BICUBIC,
|
|
|
|
Image.LANCZOS,
|
|
|
|
]:
|
|
|
|
r = self.resize(Image.new("RGB", (0, 0), "white"), (212, 195), f)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert r.mode == "RGB"
|
|
|
|
assert r.size == (212, 195)
|
|
|
|
assert 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):
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
self.resize(hopper(), (10, 10), 9)
|
2017-01-28 06:09:28 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2020-03-28 04:51:28 +03:00
|
|
|
@pytest.fixture
|
|
|
|
def gradients_image():
|
|
|
|
im = Image.open("Tests/images/radial_gradients.png")
|
|
|
|
im.load()
|
|
|
|
try:
|
|
|
|
yield im
|
|
|
|
finally:
|
|
|
|
im.close()
|
2019-12-20 17:10:40 +03:00
|
|
|
|
2020-03-28 04:51:28 +03:00
|
|
|
|
|
|
|
class TestReducingGapResize:
|
|
|
|
def test_reducing_gap_values(self, gradients_image):
|
|
|
|
ref = gradients_image.resize((52, 34), Image.BICUBIC, reducing_gap=None)
|
|
|
|
im = gradients_image.resize((52, 34), Image.BICUBIC)
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(ref, im)
|
2019-12-20 17:10:40 +03:00
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
2020-03-28 04:51:28 +03:00
|
|
|
gradients_image.resize((52, 34), Image.BICUBIC, reducing_gap=0)
|
2019-12-20 17:10:40 +03:00
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
2020-03-28 04:51:28 +03:00
|
|
|
gradients_image.resize((52, 34), Image.BICUBIC, reducing_gap=0.99)
|
2019-12-20 17:10:40 +03:00
|
|
|
|
2020-03-28 04:51:28 +03:00
|
|
|
def test_reducing_gap_1(self, gradients_image):
|
2019-12-20 17:10:40 +03:00
|
|
|
for box, epsilon in [
|
|
|
|
(None, 4),
|
|
|
|
((1.1, 2.2, 510.8, 510.9), 4),
|
|
|
|
((3, 10, 410, 256), 10),
|
|
|
|
]:
|
2020-03-28 04:51:28 +03:00
|
|
|
ref = gradients_image.resize((52, 34), Image.BICUBIC, box=box)
|
|
|
|
im = gradients_image.resize(
|
2019-12-20 17:10:40 +03:00
|
|
|
(52, 34), Image.BICUBIC, box=box, reducing_gap=1.0
|
|
|
|
)
|
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(AssertionError):
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(ref, im)
|
2019-12-20 17:10:40 +03:00
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_similar(ref, im, epsilon)
|
2019-12-20 17:10:40 +03:00
|
|
|
|
2020-03-28 04:51:28 +03:00
|
|
|
def test_reducing_gap_2(self, gradients_image):
|
2019-12-20 17:10:40 +03:00
|
|
|
for box, epsilon in [
|
|
|
|
(None, 1.5),
|
|
|
|
((1.1, 2.2, 510.8, 510.9), 1.5),
|
|
|
|
((3, 10, 410, 256), 1),
|
|
|
|
]:
|
2020-03-28 04:51:28 +03:00
|
|
|
ref = gradients_image.resize((52, 34), Image.BICUBIC, box=box)
|
|
|
|
im = gradients_image.resize(
|
2019-12-20 17:10:40 +03:00
|
|
|
(52, 34), Image.BICUBIC, box=box, reducing_gap=2.0
|
|
|
|
)
|
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(AssertionError):
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(ref, im)
|
2019-12-20 17:10:40 +03:00
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_similar(ref, im, epsilon)
|
2019-12-20 17:10:40 +03:00
|
|
|
|
2020-03-28 04:51:28 +03:00
|
|
|
def test_reducing_gap_3(self, gradients_image):
|
2019-12-20 17:10:40 +03:00
|
|
|
for box, epsilon in [
|
|
|
|
(None, 1),
|
|
|
|
((1.1, 2.2, 510.8, 510.9), 1),
|
2019-12-20 20:30:23 +03:00
|
|
|
((3, 10, 410, 256), 0.5),
|
2019-12-20 17:10:40 +03:00
|
|
|
]:
|
2020-03-28 04:51:28 +03:00
|
|
|
ref = gradients_image.resize((52, 34), Image.BICUBIC, box=box)
|
|
|
|
im = gradients_image.resize(
|
2019-12-20 17:10:40 +03:00
|
|
|
(52, 34), Image.BICUBIC, box=box, reducing_gap=3.0
|
|
|
|
)
|
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(AssertionError):
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(ref, im)
|
2019-12-20 17:10:40 +03:00
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_similar(ref, im, epsilon)
|
2019-12-20 17:10:40 +03:00
|
|
|
|
2020-03-28 04:51:28 +03:00
|
|
|
def test_reducing_gap_8(self, gradients_image):
|
2019-12-20 17:10:40 +03:00
|
|
|
for box in [None, (1.1, 2.2, 510.8, 510.9), (3, 10, 410, 256)]:
|
2020-03-28 04:51:28 +03:00
|
|
|
ref = gradients_image.resize((52, 34), Image.BICUBIC, box=box)
|
|
|
|
im = gradients_image.resize(
|
2019-12-20 17:10:40 +03:00
|
|
|
(52, 34), Image.BICUBIC, box=box, reducing_gap=8.0
|
|
|
|
)
|
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(ref, im)
|
2019-12-20 20:30:23 +03:00
|
|
|
|
2020-03-28 04:51:28 +03:00
|
|
|
def test_box_filter(self, gradients_image):
|
2019-12-20 17:10:40 +03:00
|
|
|
for box, epsilon in [
|
|
|
|
((0, 0, 512, 512), 5.5),
|
|
|
|
((0.9, 1.7, 128, 128), 9.5),
|
|
|
|
]:
|
2020-03-28 04:51:28 +03:00
|
|
|
ref = gradients_image.resize((52, 34), Image.BOX, box=box)
|
|
|
|
im = gradients_image.resize((52, 34), Image.BOX, box=box, reducing_gap=1.0)
|
2019-12-20 17:10:40 +03:00
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_similar(ref, im, epsilon)
|
2019-12-20 17:10:40 +03:00
|
|
|
|
|
|
|
|
2020-03-28 04:51:28 +03:00
|
|
|
class TestImageResize:
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_resize(self):
|
|
|
|
def resize(mode, size):
|
2014-09-05 14:03:56 +04:00
|
|
|
out = hopper(mode).resize(size)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert out.mode == mode
|
|
|
|
assert out.size == size
|
2019-06-13 18:54:24 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
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
|
Improve handling of file resources
Follow Python's file object semantics. User code is responsible for
closing resources (usually through a context manager) in a deterministic
way.
To achieve this, remove __del__ functions. These functions used to
closed open file handlers in an attempt to silence Python
ResourceWarnings. However, using __del__ has the following drawbacks:
- __del__ isn't called until the object's reference count reaches 0.
Therefore, resource handlers remain open or in use longer than
necessary.
- The __del__ method isn't guaranteed to execute on system exit. See the
Python documentation:
https://docs.python.org/3/reference/datamodel.html#object.__del__
> It is not guaranteed that __del__() methods are called for objects
> that still exist when the interpreter exits.
- Exceptions that occur inside __del__ are ignored instead of raised.
This has the potential of hiding bugs. This is also in the Python
documentation:
> Warning: Due to the precarious circumstances under which __del__()
> methods are invoked, exceptions that occur during their execution
> are ignored, and a warning is printed to sys.stderr instead.
Instead, always close resource handlers when they are no longer in use.
This will close the file handler at a specified point in the user's code
and not wait until the interpreter chooses to. It is always guaranteed
to run. And, if an exception occurs while closing the file handler, the
bug will not be ignored.
Now, when code receives a ResourceWarning, it will highlight an area
that is mishandling resources. It should not simply be silenced, but
fixed by closing resources with a context manager.
All warnings that were emitted during tests have been cleaned up. To
enable warnings, I passed the `-Wa` CLI option to Python. This exposed
some mishandling of resources in ImageFile.__init__() and
SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
|
|
|
with hopper() as im:
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
im.resize((10, 10), "unknown")
|
2019-12-07 18:08:19 +03:00
|
|
|
|
|
|
|
def test_default_filter(self):
|
|
|
|
for mode in "L", "RGB", "I", "F":
|
|
|
|
im = hopper(mode)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.resize((20, 20), Image.BICUBIC) == im.resize((20, 20))
|
2019-12-07 18:08:19 +03:00
|
|
|
|
|
|
|
for mode in "1", "P":
|
|
|
|
im = hopper(mode)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.resize((20, 20), Image.NEAREST) == im.resize((20, 20))
|