Skip to content

Validate size and rank when initializing RankFilter#9661

Merged
radarhere merged 1 commit into
python-pillow:mainfrom
radarhere:rank
Jun 19, 2026
Merged

Validate size and rank when initializing RankFilter#9661
radarhere merged 1 commit into
python-pillow:mainfrom
radarhere:rank

Conversation

@radarhere

Copy link
Copy Markdown
Member
from PIL import Image, ImageFilter
im = Image.new("L", (100, 100))
rankfilter = ImageFilter.RankFilter(2, 1)
im.filter(rankfilter)

gives

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "PIL/Image.py", line 1463, in filter
    return self._new(filter.filter(self.im))
                     ^^^^^^^^^^^^^^^^^^^^^^
  File "PIL/ImageFilter.py", line 110, in filter
    return image.rankfilter(self.size, self.rank)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: bad filter size

See

def __init__(self, size: int, rank: int) -> None:

if (!(size & 1)) {
return (Imaging)ImagingError_ValueError("bad filter size");
}

The size value used when constructing RankFilter was incorrect. However, it's only purpose is to be used as a filter.

So let's raise the error on construction, rather than letting the user think everything is ok at first.

This also applies to

if (rank < 0 || rank >= size2) {
return (Imaging)ImagingError_ValueError("bad rank value");
}

@radarhere radarhere force-pushed the rank branch 3 times, most recently from 04afc9a to 98e2b7e Compare June 6, 2026 07:46
@mergify

mergify Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@radarhere radarhere merged commit 853ece9 into python-pillow:main Jun 19, 2026
59 of 60 checks passed
@radarhere radarhere deleted the rank branch June 19, 2026 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant