diff --git a/Tests/test_image_filter.py b/Tests/test_image_filter.py index 521551212..a7932a351 100644 --- a/Tests/test_image_filter.py +++ b/Tests/test_image_filter.py @@ -23,6 +23,7 @@ from .helper import assert_image_equal, hopper ImageFilter.MinFilter, ImageFilter.ModeFilter, ImageFilter.GaussianBlur, + ImageFilter.GaussianBlur(0), ImageFilter.GaussianBlur(5), ImageFilter.GaussianBlur((2, 5)), ImageFilter.BoxBlur(0), diff --git a/src/PIL/ImageFilter.py b/src/PIL/ImageFilter.py index 0d2fec9ee..57268b8f5 100644 --- a/src/PIL/ImageFilter.py +++ b/src/PIL/ImageFilter.py @@ -170,6 +170,8 @@ class GaussianBlur(MultibandFilter): xy = self.radius if not isinstance(xy, (tuple, list)): xy = (xy, xy) + if xy == (0, 0): + return image.copy() return image.gaussian_blur(xy) @@ -202,6 +204,8 @@ class BoxBlur(MultibandFilter): xy = self.radius if not isinstance(xy, (tuple, list)): xy = (xy, xy) + if xy == (0, 0): + return image.copy() return image.box_blur(xy) diff --git a/src/libImaging/BoxBlur.c b/src/libImaging/BoxBlur.c index 41e9fbed9..adf425d0d 100644 --- a/src/libImaging/BoxBlur.c +++ b/src/libImaging/BoxBlur.c @@ -258,29 +258,39 @@ ImagingBoxBlur(Imaging imOut, Imaging imIn, float xradius, float yradius, int n) return ImagingError_ModeError(); } - imTransposed = ImagingNewDirty(imIn->mode, imIn->ysize, imIn->xsize); - if (!imTransposed) { - return NULL; - } - /* Apply blur in one dimension. Use imOut as a destination at first pass, then use imOut as a source too. */ - ImagingHorizontalBoxBlur(imOut, imIn, xradius); - for (i = 1; i < n; i++) { - ImagingHorizontalBoxBlur(imOut, imOut, xradius); - } - /* Transpose result for blur in another direction. */ - ImagingTranspose(imTransposed, imOut); - /* Reuse imTransposed as a source and destination there. */ - for (i = 0; i < n; i++) { - ImagingHorizontalBoxBlur(imTransposed, imTransposed, yradius); + if (xradius != 0) { + ImagingHorizontalBoxBlur(imOut, imIn, xradius); + for (i = 1; i < n; i++) { + ImagingHorizontalBoxBlur(imOut, imOut, xradius); + } } - /* Restore original orientation. */ - ImagingTranspose(imOut, imTransposed); + if (yradius != 0) { + imTransposed = ImagingNewDirty(imIn->mode, imIn->ysize, imIn->xsize); + if (!imTransposed) { + return NULL; + } - ImagingDelete(imTransposed); + /* Transpose result for blur in another direction. */ + ImagingTranspose(imTransposed, xradius == 0 ? imIn : imOut); + + /* Reuse imTransposed as a source and destination there. */ + for (i = 0; i < n; i++) { + ImagingHorizontalBoxBlur(imTransposed, imTransposed, yradius); + } + /* Restore original orientation. */ + ImagingTranspose(imOut, imTransposed); + + ImagingDelete(imTransposed); + } + if (xradius == 0 && yradius == 0) { + if (!ImagingCopy2(imOut, imIn)) { + return NULL; + } + } return imOut; }