Skip to content

Gaussian/Box blur with different X and Y radius #7321

@Splendide-Imaginarius

Description

@Splendide-Imaginarius

What did you do?

Call ImageFilter.BoxBlur or ImageFilter.GaussianBlur with the radius parameter set to a 2-tuple containing an X and Y radius.

What did you expect to happen?

The blur operation should use different radii for the X and Y dimensions. If one of the dimensions is zero, then the blur should only be done in the other axis.

What actually happened?

This is not supported, per documentation (the radius must be a single number).

What are your OS, Python and Pillow versions?

  • OS: Linux
  • Python: 3.11
  • Pillow: 9.5.0

Example code

# im is an image

# This works
im.filter(ImageFilter.GaussianBlur(32))

# This doesn't work
im.filter(ImageFilter.GaussianBlur((32, 16)))

Other notes

It looks like there's already internal code for doing single-dimensional box blur; making the 2-dimensional blur functions (which are exposed to Python) use different radii per dimension should be pretty easy. See this code:

/* Apply blur in one dimension.
Use imOut as a destination at first pass,
then use imOut as a source too. */
ImagingHorizontalBoxBlur(imOut, imIn, radius);
for (i = 1; i < n; i++) {
ImagingHorizontalBoxBlur(imOut, imOut, radius);
}
/* 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, radius);
}
/* Restore original orientation. */
ImagingTranspose(imOut, imTransposed);

I would probably be happy to send in a PR, if this is a feature that you would welcome -- let me know.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions