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

Division by zero on smart crop #350

Closed
awidgery opened this issue Jan 16, 2015 · 5 comments
Closed

Division by zero on smart crop #350

awidgery opened this issue Jan 16, 2015 · 5 comments
Labels
Milestone

Comments

@awidgery
Copy link

I'm getting a float division by zero error here:

sorl/thumbnail/engines/pil_engine.py in _get_image_entropy, line 239

I'm calling it using:

{% thumbnail comment.user.profile.avatar_image "30x30" crop="smart" as im %}

This happens on some images and not others. The image that's causing me issues is this one:

https://www.dropbox.com/s/m5e79cvk53ujs7b/Chelsea_physic_garden.jpg?dl=0

From a brief bit of digging the code is:

def _get_image_entropy(self, image):
    """calculate the entropy of an image"""
    hist = image.histogram()
    hist_size = sum(hist)
    print hist_size
    hist = [float(h) / hist_size for h in hist]
    return -sum([p * math.log(p, 2) for p in hist if p != 0])

print hist_size gives a zero in the console - hence the error. We need some sort of try/except on this calculation, but I'm not sure what the except should be!

@awidgery
Copy link
Author

Should have said I'm using sorl-thumbnail==11.12.1b

I've also noticed that if I set the image larger I don't get this error. Narrowing it down, this gives the error:

{% thumbnail comment.user.profile.avatar_image "37x37" crop="smart" as im %}

...but one pixel bigger does not:

{% thumbnail comment.user.profile.avatar_image "38x38" crop="smart" as im %}

I presumably that's because when the image is very small the entropy distinction tends towards zero. Which suggests the entropy calc is being run on an already-reduced image? Surely the entropy cropper should be run on the full-size image to determine the "smartest" area - and then resized down to the desired size?

@relekang relekang added this to the Version 12.3 milestone Jan 16, 2015
@relekang relekang added the bug label Jan 16, 2015
@Arti3DPlayer
Copy link

Have the same bug

@mrkre
Copy link

mrkre commented Feb 1, 2016

Same bug is reproduced when calling get_thumbnail from sorl.thumbnail even when crop='center' is used

@lampslave
Copy link
Contributor

Try with pillow==3.1.0. See #429.

@mrkre
Copy link

mrkre commented Feb 1, 2016

@lampslave Thanks. Solves the issue.

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

No branches or pull requests

6 participants