Django, sorl-thumbnail crop picture head
Asked Answered
B

4

4

guys, i would like to know if sorl-thumbnail have any option to crop from the bottom to the top... i have a litter problem, in some picture sorl-thumbnail is croping the head of the people in pictures.

Thanks

Biltong answered 4/10, 2009 at 15:55 Comment(0)
C
5

I've just released a new version of sorl-thumbnail (3.2.5) with cropping from edge and smart cropping inspired by btol45's answer.

Quoting the docs:

By default, the image is centered before being cropped. To crop from the edges, pass a comma separated string containing the x and y percentage offsets (negative values go from the right/bottom). Some examples follow:

  • crop="0,0" will crop from the left and top edges.

  • crop="-10,-0" will crop from the right edge (with a 10% offset) and the bottom edge.

  • crop=",0" will keep the default behavior for the x axis (horizontally centering the image) and crop from the top edge.

The image can also be "smart cropped" by using crop="smart". The image is incrementally cropped down to the requested size by removing slices from edges with the least entropy.

Chaetognath answered 4/10, 2009 at 22:39 Comment(3)
Note that this answer is for an ancient version of sorl-thumbnail, and will not work with any of the more recent versions.Fontainebleau
It'll work for easy-thumbnails still ;)Chaetognath
I added an answer that works in in recent sorl versions. In short, crop="center top".Usury
N
5

I don't believe this is built into solr-thumbnails yet, but here's a plugin I cribbed from reddit that accomplishes what you're after. It's not perfect, but it tends to get the job done. It doesn't crop from bottom to top, but rather uses the entropy of the slices to determine what end to crop from. It's a slight improvement on the reddit version as it handles either portrait or landscape images.

import Image, ImageFile, math
#from ImageEnhance import Color
#import os, sys


def image_entropy(im):
    """From Reddit: Calculate the entropy of an image"""
    hist = im.histogram()
    hist_size = sum(hist)
    hist = [float(h) / hist_size for h in hist]
    return -sum([p * math.log(p, 2) for p in hist if p != 0])

def square_image(im, requested_size, opts):
    """From Reddit: if the image is taller than it is wide, square it off. determine
    which pieces to cut off based on the entropy pieces.

    This version is improved as it squares images that are wider than it is tall.
    """
    if 'autosquare' in opts:
        x,y = im.size

        # if the image is taller than it is wide:
        if y > x:
            while y > x:
                #slice 10px at a time until square
                slice_height = min(y - x, 10)

                bottom = im.crop((0, y - slice_height, x, y))
                top = im.crop((0, 0, x, slice_height))

                #remove the slice with the least entropy
                if image_entropy(bottom) < image_entropy(top):
                    im = im.crop((0, 0, x, y - slice_height))
                else:
                    im = im.crop((0, slice_height, x, y))

                x,y = im.size

        # If the image is wider than it is tall
        else:
            while y < x:
                #slice 10px at a time until square
                slice_width = min(x - y, 10)

                left = im.crop((0,0, y, slice_width))
                right = im.crop((0,y - slice_width, x, y))

                #remove the slice with the least entropy
                if image_entropy(left) < image_entropy(right):
                    im = im.crop((0, 0, x - slice_width, y))
                else:
                    im = im.crop((slice_width, 0, x, y))

                x,y = im.size

        im = im.resize(requested_size, resample=Image.ANTIALIAS)

    return im
square_image.valid_options = ('autosquare',) 
Nugatory answered 4/10, 2009 at 19:25 Comment(2)
Just a note: you can see a non-solr version here: github.com/wiremine/Python-Square-Thumbnail-CreatorNugatory
Just a note: it's sorl, not solr ;)Chaetognath
C
5

I've just released a new version of sorl-thumbnail (3.2.5) with cropping from edge and smart cropping inspired by btol45's answer.

Quoting the docs:

By default, the image is centered before being cropped. To crop from the edges, pass a comma separated string containing the x and y percentage offsets (negative values go from the right/bottom). Some examples follow:

  • crop="0,0" will crop from the left and top edges.

  • crop="-10,-0" will crop from the right edge (with a 10% offset) and the bottom edge.

  • crop=",0" will keep the default behavior for the x axis (horizontally centering the image) and crop from the top edge.

The image can also be "smart cropped" by using crop="smart". The image is incrementally cropped down to the requested size by removing slices from edges with the least entropy.

Chaetognath answered 4/10, 2009 at 22:39 Comment(3)
Note that this answer is for an ancient version of sorl-thumbnail, and will not work with any of the more recent versions.Fontainebleau
It'll work for easy-thumbnails still ;)Chaetognath
I added an answer that works in in recent sorl versions. In short, crop="center top".Usury
P
4

This question is old, but, since it appears as the first result in Google when searching for django smart crop, I want to add my little grain.

This "crop=auto" feature got added to sorl, but later, it got removed again. So, for others who might come with this need, you can try:

https://github.com/francescortiz/image

which allows you to set the center of attention of an image through the admin.

Provencal answered 6/5, 2012 at 10:5 Comment(0)
U
1

While the original answer no longer works, in recent versions of sorl, you can specify space-separated x and y crop values. For example, crop="center top", will center in X but keep the top in Y, which was better for pictures of people in my case, but not perfect.

Usury answered 13/12, 2012 at 19:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.