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

Fixes #773: Accept a tuple as blur radius and ensure to call PIL blur filter with a tuple #774

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 5 additions & 2 deletions sorl/thumbnail/engines/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,11 @@ def blur(self, image, geometry, options):
"""
Wrapper for ``_blur``
"""
if options.get('blur'):
return self._blur(image, int(options.get('blur')))
radius = options.get('blur')
if radius:
if isinstance(radius, str):
radius = int(radius)
return self._blur(image, radius)
return image

def padding(self, image, geometry, options):
Expand Down
5 changes: 4 additions & 1 deletion sorl/thumbnail/engines/pil_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ def __init__(self, radius=2):
self.radius = radius

def filter(self, image):
return image.gaussian_blur(self.radius)
xy = self.radius
if isinstance(xy, (int, float)):
xy = (xy, xy)
return image.gaussian_blur(xy)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about importing GaussianBlur from PIL.ImageFilter instead of copy-pasting pillow code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The difference between those two classes was the base class, pillows GaussianBlur is a MultibandFilter, although the only occurance of ImageFilter.Filter in pil_engine.py is GaussianBlur. Maybe there was a reason for that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestingly, the ImageFilter is only imported when no ImportError happens, i.E. PIL is available. Would it make sense to change this and import the GaussianBlur from Pillow in this PR? Or is that a case for another refactoring PR?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to test some refactoring in a separate PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, I'd love to see some test around this functionality (I think there are no currently).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added an engine test in the SimpleTestCase and changed the pil_engine to use pillows GaussianBlur filter. I ran tox locally, but only with python 3.12.

I noticed that the other engines don't support blur anyways (so i borrowed the pil_engine unittest skipif). But each library has support for some kind of blur. I'd be happy to contribute to this feature with docs and tests in another PR.



class Engine(EngineBase):
Expand Down