Sorl-thumbnail: Division by zero on smart crop

Created on 16 Jan 2015  路  5Comments  路  Source: jazzband/sorl-thumbnail

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!

bug

Most helpful comment

Try with pillow==3.1.0. See #429.

All 5 comments

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?

Have the same bug

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

Try with pillow==3.1.0. See #429.

@lampslave Thanks. Solves the issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kevin-brown picture kevin-brown  路  12Comments

devova picture devova  路  8Comments

ghost picture ghost  路  3Comments

nuschk picture nuschk  路  7Comments

uri-rodberg picture uri-rodberg  路  11Comments