Skip to content

Commit

Permalink
Merge pull request #5411 from radarhere/resize_default
Browse files Browse the repository at this point in the history
For special image modes, revert default resize resampling to NEAREST
  • Loading branch information
hugovk authored Apr 19, 2021
2 parents fc08a72 + 7c0344b commit d2e73b0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
4 changes: 4 additions & 0 deletions Tests/test_image_resize.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,7 @@ def test_default_filter(self):
for mode in "1", "P":
im = hopper(mode)
assert im.resize((20, 20), Image.NEAREST) == im.resize((20, 20))

for mode in "I;16", "I;16L", "I;16B", "BGR;15", "BGR;16":
im = hopper(mode)
assert im.resize((20, 20), Image.NEAREST) == im.resize((20, 20))
15 changes: 10 additions & 5 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1849,7 +1849,7 @@ def _get_safe_box(self, size, resample, box):
min(self.size[1], math.ceil(box[3] + support_y)),
)

def resize(self, size, resample=BICUBIC, box=None, reducing_gap=None):
def resize(self, size, resample=None, box=None, reducing_gap=None):
"""
Returns a resized copy of this image.
Expand All @@ -1859,9 +1859,11 @@ def resize(self, size, resample=BICUBIC, box=None, reducing_gap=None):
one of :py:data:`PIL.Image.NEAREST`, :py:data:`PIL.Image.BOX`,
:py:data:`PIL.Image.BILINEAR`, :py:data:`PIL.Image.HAMMING`,
:py:data:`PIL.Image.BICUBIC` or :py:data:`PIL.Image.LANCZOS`.
Default filter is :py:data:`PIL.Image.BICUBIC`.
If the image has mode "1" or "P", it is
always set to :py:data:`PIL.Image.NEAREST`.
If the image has mode "1" or "P", it is always set to
:py:data:`PIL.Image.NEAREST`.
If the image mode specifies a number of bits, such as "I;16", then the
default filter is :py:data:`PIL.Image.NEAREST`.
Otherwise, the default filter is :py:data:`PIL.Image.BICUBIC`.
See: :ref:`concept-filters`.
:param box: An optional 4-tuple of floats providing
the source image region to be scaled.
Expand All @@ -1882,7 +1884,10 @@ def resize(self, size, resample=BICUBIC, box=None, reducing_gap=None):
:returns: An :py:class:`~PIL.Image.Image` object.
"""

if resample not in (NEAREST, BILINEAR, BICUBIC, LANCZOS, BOX, HAMMING):
if resample is None:
type_special = ";" in self.mode
resample = NEAREST if type_special else BICUBIC
elif resample not in (NEAREST, BILINEAR, BICUBIC, LANCZOS, BOX, HAMMING):
message = f"Unknown resampling filter ({resample})."

filters = [
Expand Down

0 comments on commit d2e73b0

Please sign in to comment.