SmileyChris

sorl-thumbnail, now with smarter cropping

Rambling on about Django 60 months ago.

I just committed two additions to functionality of the "crop" option in sorl-thumbnail and released it on PYPI as version 3.2.5.

The first is the ability to crop from an edge, requested quite some time ago.

The second is functionality to "smart crop" the image, incrementally cropping it down to the requested size by removing slices from edges with the least entropy, inspired from a question and answer on StackOverflow.

Leave a comment or see my other ramblings.

Comments (7)

Paul said: (60 months ago)

does the smart crop work as a field option as well? eg:
ImageWithThumbnailsField(
upload_to = 'uploads/blog_img',
thumbnail = {'size': (89, 89), 'options': ['crop="smart"']},

the docs aren't clear.

p.s. go kiwi!

:) Chris said: (60 months ago)

Hi Paul,

They work as a field option - the whole dictionary is the options, so each option should just be a key: {'size': (89, 89), 'crop': 'smart'}

Paul said: (60 months ago)

I tried:
mainimg = ImageWithThumbnailsField(
upload_to = 'uploads/blog_img',
thumbnail = {'size': (180, 102), 'crop': 'smart'},

but now I get:
Exception Value:
Invalid attr 'size' found in 'thumbnail' arg

bit weird. no error if I take the 'crop':'smart' out altogether, but I'd like to use it.

Paul said: (60 months ago)

in the docs they do:
extra_thumbnails={
'icon': {'size': (16, 16), 'options': ['crop', 'upscale']},
'large': {'size': (200, 400)},
},

this works for me:
mainimg = ImageWithThumbnailsField(
upload_to = 'uploads/blog_img',
thumbnail = {'size': (180, 102), 'options': ['crop']},

but it's not smart cropping then of course

:) Chris said: (60 months ago)

Hang on, I think that was wrong advice I gave before. Try: {'size': (89, 89), 'options': {'crop': 'smart'}}

Paul said: (60 months ago)

cheers, works!

tezro said: (54 months ago)

Damn, 'options': {'crop': 'smart'} — totally great! Thanks.

Leave a comment