2019-02-04 22:15:50 +03:00
|
|
|
from PIL import Image
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-11-24 15:22:13 +03:00
|
|
|
from .helper import PillowTestCase, fromstring, hopper, tostring
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
class TestImageThumbnail(PillowTestCase):
|
|
|
|
def test_sanity(self):
|
2014-09-05 14:03:56 +04:00
|
|
|
im = hopper()
|
2019-12-07 20:07:27 +03:00
|
|
|
self.assertIsNone(im.thumbnail((100, 100)))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-12-07 20:07:27 +03:00
|
|
|
self.assertEqual(im.size, (100, 100))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_aspect(self):
|
2019-12-07 20:07:27 +03:00
|
|
|
im = Image.new("L", (128, 128))
|
2014-06-10 13:10:47 +04:00
|
|
|
im.thumbnail((100, 100))
|
2019-12-07 20:07:27 +03:00
|
|
|
self.assertEqual(im.size, (100, 100))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-12-07 20:07:27 +03:00
|
|
|
im = Image.new("L", (128, 256))
|
2014-06-10 13:10:47 +04:00
|
|
|
im.thumbnail((100, 100))
|
2019-12-07 20:07:27 +03:00
|
|
|
self.assertEqual(im.size, (50, 100))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-12-07 20:07:27 +03:00
|
|
|
im = Image.new("L", (128, 256))
|
2014-06-10 13:10:47 +04:00
|
|
|
im.thumbnail((50, 100))
|
2019-12-07 20:07:27 +03:00
|
|
|
self.assertEqual(im.size, (50, 100))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-12-07 20:07:27 +03:00
|
|
|
im = Image.new("L", (256, 128))
|
2014-06-10 13:10:47 +04:00
|
|
|
im.thumbnail((100, 100))
|
2019-12-07 20:07:27 +03:00
|
|
|
self.assertEqual(im.size, (100, 50))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-12-07 20:07:27 +03:00
|
|
|
im = Image.new("L", (256, 128))
|
2014-06-10 13:10:47 +04:00
|
|
|
im.thumbnail((100, 50))
|
2019-12-07 20:07:27 +03:00
|
|
|
self.assertEqual(im.size, (100, 50))
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2019-12-07 20:07:27 +03:00
|
|
|
im = Image.new("L", (128, 128))
|
2014-06-10 13:10:47 +04:00
|
|
|
im.thumbnail((100, 100))
|
2019-12-07 20:07:27 +03:00
|
|
|
self.assertEqual(im.size, (100, 100))
|
|
|
|
|
|
|
|
im = Image.new("L", (256, 162)) # ratio is 1.5802469136
|
|
|
|
im.thumbnail((33, 33))
|
|
|
|
self.assertEqual(im.size, (33, 21)) # ratio is 1.5714285714
|
|
|
|
|
|
|
|
im = Image.new("L", (162, 256)) # ratio is 0.6328125
|
|
|
|
im.thumbnail((33, 33))
|
|
|
|
self.assertEqual(im.size, (21, 33)) # ratio is 0.6363636364
|
2019-02-04 22:15:50 +03:00
|
|
|
|
2019-12-16 22:05:36 +03:00
|
|
|
def test_float(self):
|
|
|
|
im = Image.new("L", (128, 128))
|
|
|
|
im.thumbnail((99.9, 99.9))
|
|
|
|
self.assertEqual(im.size, (100, 100))
|
|
|
|
|
2019-02-04 22:15:50 +03:00
|
|
|
def test_no_resize(self):
|
|
|
|
# Check that draft() can resize the image to the destination size
|
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 Image.open("Tests/images/hopper.jpg") as im:
|
|
|
|
im.draft(None, (64, 64))
|
|
|
|
self.assertEqual(im.size, (64, 64))
|
2019-02-04 22:15:50 +03:00
|
|
|
|
|
|
|
# Test thumbnail(), where only draft() is necessary to resize the image
|
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 Image.open("Tests/images/hopper.jpg") as im:
|
|
|
|
im.thumbnail((64, 64))
|
2019-12-07 20:07:27 +03:00
|
|
|
self.assertEqual(im.size, (64, 64))
|
2019-11-24 05:24:00 +03:00
|
|
|
|
|
|
|
def test_DCT_scaling_edges(self):
|
2019-11-24 15:22:13 +03:00
|
|
|
# Make an image with red borders and size (N * 8) + 1 to cross DCT grid
|
2019-11-24 15:34:12 +03:00
|
|
|
im = Image.new("RGB", (257, 257), "red")
|
2019-12-17 02:25:40 +03:00
|
|
|
im.paste(Image.new("RGB", (235, 235)), (11, 11))
|
2019-11-24 05:24:00 +03:00
|
|
|
|
2019-11-24 15:34:12 +03:00
|
|
|
thumb = fromstring(tostring(im, "JPEG", quality=99, subsampling=0))
|
2019-12-20 14:59:18 +03:00
|
|
|
# small reducing_gap to amplify the effect
|
|
|
|
thumb.thumbnail((32, 32), Image.BICUBIC, reducing_gap=1.0)
|
2019-11-24 05:24:00 +03:00
|
|
|
|
2019-11-24 15:34:12 +03:00
|
|
|
ref = im.resize((32, 32), Image.BICUBIC)
|
2019-12-17 02:25:40 +03:00
|
|
|
# This is still JPEG, some error is present. Without the fix it is 11.5
|
|
|
|
self.assert_image_similar(thumb, ref, 1.5)
|
2019-12-20 17:10:40 +03:00
|
|
|
|
|
|
|
def test_reducing_gap_values(self):
|
|
|
|
im = hopper()
|
|
|
|
im.thumbnail((18, 18), Image.BICUBIC)
|
|
|
|
|
|
|
|
ref = hopper()
|
|
|
|
ref.thumbnail((18, 18), Image.BICUBIC, reducing_gap=2.0)
|
|
|
|
# reducing_gap=2.0 should be the default
|
|
|
|
self.assert_image_equal(ref, im)
|
|
|
|
|
|
|
|
ref = hopper()
|
|
|
|
ref.thumbnail((18, 18), Image.BICUBIC, reducing_gap=None)
|
|
|
|
with self.assertRaises(AssertionError):
|
|
|
|
self.assert_image_equal(ref, im)
|
|
|
|
|
|
|
|
self.assert_image_similar(ref, im, 3.5)
|
|
|
|
|
|
|
|
def test_reducing_gap_for_DCT_scaling(self):
|
|
|
|
with Image.open("Tests/images/hopper.jpg") as ref:
|
|
|
|
# thumbnail should call draft with reducing_gap scale
|
|
|
|
ref.draft(None, (18 * 3, 18 * 3))
|
|
|
|
ref = ref.resize((18, 18), Image.BICUBIC)
|
|
|
|
|
|
|
|
with Image.open("Tests/images/hopper.jpg") as im:
|
|
|
|
im.thumbnail((18, 18), Image.BICUBIC, reducing_gap=3.0)
|
|
|
|
|
|
|
|
self.assert_image_equal(ref, im)
|