Skip to content

Commit

Permalink
Fixes #773: Accept a tuple as blur radius use GaussianBlur from PIL
Browse files Browse the repository at this point in the history
  • Loading branch information
SvenBroeckling authored Oct 22, 2024
1 parent 606254a commit 594418a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
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
13 changes: 2 additions & 11 deletions sorl/thumbnail/engines/pil_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from sorl.thumbnail.engines.base import EngineBase

try:
from PIL import Image, ImageDraw, ImageFile, ImageFilter, ImageMode
from PIL import Image, ImageDraw, ImageFile, ImageMode
from PIL.ImageFilter import GaussianBlur
except ImportError:
import Image
import ImageDraw
Expand Down Expand Up @@ -68,16 +69,6 @@ def round_rectangle(size, radius, fill):
return rectangle


class GaussianBlur(ImageFilter.Filter):
name = "GaussianBlur"

def __init__(self, radius=2):
self.radius = radius

def filter(self, image):
return image.gaussian_blur(self.radius)


class Engine(EngineBase):
def get_image(self, source):
buffer = BytesIO(source.read())
Expand Down
9 changes: 9 additions & 0 deletions tests/thumbnail_tests/test_engines.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ def test_upscale_and_crop(self):
self.assertEqual(t.x, 400)
self.assertEqual(t.y, 300)

@unittest.skipIf('pil_engine' not in settings.THUMBNAIL_ENGINE, 'blur is only supported in PIL')
def test_crop_and_blur(self):
item = Item.objects.get(image='200x100.jpg')

t = self.BACKEND.get_thumbnail(item.image, '100x100', crop="center", blur='3')

self.assertEqual(t.x, 100)
self.assertEqual(t.y, 100)

def test_kvstore(self):
im = ImageFile(Item.objects.get(image='500x500.jpg').image)
self.KVSTORE.delete_thumbnails(im)
Expand Down

0 comments on commit 594418a

Please sign in to comment.