Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Tests/test_image_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,13 @@ def test_invalid_box_blur_filter(radius: int | tuple[int, int]) -> None:
box_blur_filter.radius = radius
with pytest.raises(ValueError):
im.filter(box_blur_filter)


def test_rankfilter_size_1() -> None:
im = Image.new("L", (3, 3), 128)

# Size 1 should not crash (margin is 0)
assert im.filter(ImageFilter.MinFilter(1)).getpixel((1, 1)) == 128
assert im.filter(ImageFilter.MaxFilter(1)).getpixel((1, 1)) == 128
assert im.filter(ImageFilter.MedianFilter(1)).getpixel((1, 1)) == 128
assert im.filter(ImageFilter.RankFilter(1, 0)).getpixel((1, 1)) == 128
3 changes: 3 additions & 0 deletions src/_imaging.c
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,9 @@ _expand_image(ImagingObject *self, PyObject *args) {
return NULL;
}

if (m == 0) {
return PyImagingNew(ImagingCopy(self->image));
}
return PyImagingNew(ImagingExpand(self->image, m));
}

Expand Down
2 changes: 1 addition & 1 deletion src/libImaging/Filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ ImagingExpand(Imaging imIn, int margin) {
if (margin < 0) {
return (Imaging)ImagingError_ValueError("bad kernel size");
}
if (margin > INT_MAX / (margin * (int)sizeof(FLOAT32))) {
if (margin > 0 && margin > INT_MAX / (margin * (int)sizeof(FLOAT32))) {
return (Imaging)ImagingError_ValueError("filter size too large");
}

Expand Down
Loading