Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Splendide-Imaginarius opened this issue Aug 5, 2023 · 3 comments · Fixed by #7336
Closed

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

Splendide-Imaginarius opened this issue Aug 5, 2023 · 3 comments · Fixed by #7336

Comments

@Splendide-Imaginarius
Copy link

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.

@homm
Copy link
Member

homm commented Aug 5, 2023

Personally, I absolutely welcome for this feature.

@radarhere
Copy link
Member

I've created PR #7336 to resolve this.

@Splendide-Imaginarius
Copy link
Author

Nice, thank you for saving me the trouble @radarhere :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants