From ac92836e8141d587a7a7f5e5a3af7bdde7ba5574 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sat, 7 Dec 2019 18:08:19 +0300 Subject: [PATCH] Change default resize resampling filter --- Tests/test_file_gif.py | 2 +- Tests/test_image_getdata.py | 4 +++- Tests/test_image_resize.py | 9 +++++++++ Tests/test_imagedraw.py | 3 ++- Tests/test_imagefile.py | 2 +- docs/releasenotes/7.0.0.rst | 7 +++++++ src/PIL/Image.py | 7 ++++--- 7 files changed, 27 insertions(+), 7 deletions(-) diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index b17459a8b..bbd589ada 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -754,7 +754,7 @@ class TestFileGif(PillowTestCase): def test_getdata(self): # test getheader/getdata against legacy values # Create a 'P' image with holes in the palette - im = Image._wedge().resize((16, 16)) + im = Image._wedge().resize((16, 16), Image.NEAREST) im.putpalette(ImagePalette.ImagePalette("RGB")) im.info = {"background": 0} diff --git a/Tests/test_image_getdata.py b/Tests/test_image_getdata.py index d9bfcc7dd..18d381390 100644 --- a/Tests/test_image_getdata.py +++ b/Tests/test_image_getdata.py @@ -1,3 +1,5 @@ +from PIL import Image + from .helper import PillowTestCase, hopper @@ -13,7 +15,7 @@ class TestImageGetData(PillowTestCase): def test_roundtrip(self): def getdata(mode): - im = hopper(mode).resize((32, 30)) + im = hopper(mode).resize((32, 30), Image.NEAREST) data = im.getdata() return data[0], len(data), len(list(data)) diff --git a/Tests/test_image_resize.py b/Tests/test_image_resize.py index 2538dd378..091655827 100644 --- a/Tests/test_image_resize.py +++ b/Tests/test_image_resize.py @@ -150,3 +150,12 @@ class TestImageResize(PillowTestCase): # Test unknown resampling filter with hopper() as im: self.assertRaises(ValueError, im.resize, (10, 10), "unknown") + + def test_default_filter(self): + for mode in "L", "RGB", "I", "F": + im = hopper(mode) + self.assertEqual(im.resize((20, 20), Image.BICUBIC), im.resize((20, 20))) + + for mode in "1", "P": + im = hopper(mode) + self.assertEqual(im.resize((20, 20), Image.NEAREST), im.resize((20, 20))) diff --git a/Tests/test_imagedraw.py b/Tests/test_imagedraw.py index d3791e0b1..4535a4838 100644 --- a/Tests/test_imagedraw.py +++ b/Tests/test_imagedraw.py @@ -159,7 +159,8 @@ class TestImageDraw(PillowTestCase): # Arrange im = Image.new("RGB", (W, H)) draw = ImageDraw.Draw(im) - with Image.open("Tests/images/pil123rgba.png").resize((50, 50)) as small: + with Image.open("Tests/images/pil123rgba.png") as small: + small = small.resize((50, 50), Image.NEAREST) # Act draw.bitmap((10, 10), small) diff --git a/Tests/test_imagefile.py b/Tests/test_imagefile.py index 50c7d337b..f575c8c1a 100644 --- a/Tests/test_imagefile.py +++ b/Tests/test_imagefile.py @@ -24,7 +24,7 @@ class TestImageFile(PillowTestCase): def test_parser(self): def roundtrip(format): - im = hopper("L").resize((1000, 1000)) + im = hopper("L").resize((1000, 1000), Image.NEAREST) if format in ("MSP", "XBM"): im = im.convert("1") diff --git a/docs/releasenotes/7.0.0.rst b/docs/releasenotes/7.0.0.rst index 386a26757..89a0c5a4e 100644 --- a/docs/releasenotes/7.0.0.rst +++ b/docs/releasenotes/7.0.0.rst @@ -47,6 +47,13 @@ Setting the size of TIFF images Setting the size of a TIFF image directly (eg. ``im.size = (256, 256)``) throws an error. Use ``Image.resize`` instead. +Default resize resampling filter +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +The default resampling filter for the ``Image.resize`` method is changed to +high-quality convolution ``Image.BICUBIC`` instead of ``Image.NEAREST``. +``Image.NEAREST`` is still always used for images in "P" and "1" modes. + API Changes =========== diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 96ec314bc..a509b13cb 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1761,7 +1761,7 @@ class Image: return m_im - def resize(self, size, resample=NEAREST, box=None): + def resize(self, size, resample=BICUBIC, box=None): """ Returns a resized copy of this image. @@ -1771,8 +1771,9 @@ class Image: one of :py:attr:`PIL.Image.NEAREST`, :py:attr:`PIL.Image.BOX`, :py:attr:`PIL.Image.BILINEAR`, :py:attr:`PIL.Image.HAMMING`, :py:attr:`PIL.Image.BICUBIC` or :py:attr:`PIL.Image.LANCZOS`. - If omitted, or if the image has mode "1" or "P", it is - set :py:attr:`PIL.Image.NEAREST`. + Default filter is :py:attr:`PIL.Image.BICUBIC`. + If the image has mode "1" or "P", it is + always set :py:attr:`PIL.Image.NEAREST`. See: :ref:`concept-filters`. :param box: An optional 4-tuple of floats giving the region of the source image which should be scaled.