Django, head with a pattern

Guys, I would like to know if sorl-thumbnail has any option to crop from the bottom up ... I have a problem with garbage, in some image the image of straw twists the heads of people in photos.

thanks

+3
source share
4 answers

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

Quoting documents:

By default, the image is centered before cropping. To harvest with ribs, pass a comma-separated string containing xand yinterest offset (negative values are right / bottom). Some examples:

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

  • crop="-10,-0" ( 10% ) .

  • crop=",0" x ( ) .

" " crop="smart". , .

+5

, solr-thumbnails, , reddit, , . , . , , , . reddit, , .

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',) 
+5

, Google - django, .

"crop = " ​​ sorl, . , , , :

https://github.com/francescortiz/image

.

+3

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

+1
source

All Articles